現在、写真はギャラリーインターフェイスからのみアクセスできるフォトギャラリーアプリケーションを作成しようとしています。ギャラリー=> has_many:写真、写真=> belongs_to:ギャラリー。これはすべて正常に動作しています。ネストされたモデルのレールとペーパークリップ:フォームからのアップロードが機能しない
しかし、今では写真を添付ファイルに追加しようとしています:画像。私はhis tutorialの中でニースが言うことをすべてやった。そして、私はvalidates_attachment_presence:imageを追加したばかりだ。確認の前に、画像を保存した後に画像が表示されないという点を除いて、写真モデルは正常に動作していました。アップロードする画像を選択した後、バリデーションを行ったところ、次のようになります:flash =>
1 error prohibited this photo from being saved
There were problems with the following fields:
* Image file name must be set.
ここでは何が起こっていますか?下記の関連コード:
モデル/写真
class Photo < ActiveRecord::Base
attr_accessible :gallery_id, :name, :rating
belongs_to :gallery
validates_associated :gallery
has_attached_file :image
validates_attachment_presence :image
end
ビュー/写真/ _form.html.erb
<% form_for [@gallery, @photo], :html => { :multipart => true } do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<% if @photo.image? %>
<%= image_tag @photo.image.url %><br />
<%= link_to @photo.image.url, @photo.image.url %>
<% end %>
<%= f.label :image %><br />
<%= f.file_field :image %>
</p>
<p><%= f.submit %></p>
<% end %>
モデル/ gallery.rb
class Gallery < ActiveRecord::Base
attr_accessible :name, :user_id, :shoot_date
# destroy all photos when a gallery is destroyed
has_many :photos, :dependent => :destroy
end
私は」だと思います以前はマルチパートフォームを正しく設定していましたが、モデルを入れ子にしないでクリップクリップモデルを試してみる前に、私はこの問題を抱えていたと思います。何か不足していますか?
更新:
Processing PhotosController#update (for 127.0.0.1 at 2010-12-05 14:19:29) [PUT]
Parameters: {"photo"=>{"name"=>"blah", "image"=>#<File:/tmp/RackMultipart20101205-2909-wo2g7z-0>}, "commit"=>"Save changes", "id"=>"10", "gallery_id"=>"3"}
Gallery Columns (0.6ms) SHOW FIELDS FROM `galleries`
Gallery Load (0.1ms) SELECT * FROM `galleries` WHERE (`galleries`.`id` = 3)
Photo Columns (0.7ms) SHOW FIELDS FROM `photos`
Photo Load (0.1ms) SELECT * FROM `photos` WHERE (`photos`.`id` = 10 AND (`photos`.gallery_id = 3))
WARNING: Can't mass-assign these protected attributes: image
SQL (0.1ms) BEGIN
CACHE (0.0ms) SELECT * FROM `galleries` WHERE (`galleries`.`id` = 3)
SQL (0.1ms) ROLLBACK
Rendering template within layouts/application
Rendering photos/edit
Rendered photos/_form (64.1ms)
Completed in 83ms (View: 67, DB: 2) | 200 OK [http://localhost/galleries/3/photos/10]