我有一个系统设置,用户可以在其中发布微博(基本上是状态更新)并上传照片以与微博一起使用。
我有:
微博模型(has_one photo) Photo_album 模型(has_many photos) 照片模型(belongs_to micropost, belongs_to photo_album)
用户在文本区域中填写文本并选择一张照片。提交后,微博表会使用微博相关数据(例如内容、created_at 等)进行更新。
同时,我希望我的照片表(照片模型)更新为用户选择的照片但正确的相册。如果您看下面,您可以在我的 users_controller 中看到实例变量 @photo 的值。这样可以确保上传的照片链接到名为“微博相册”的正确相册。它的目的是链接到所有与微博相关的照片。
我有一个用户控制器:
def new
@user = User.new
@micropost = Micropost.new(:user_id => users_id)
@photo = Photo.new(:photo_album_id => PhotoAlbum.where(:user_id => current_user.id, :album_title => "microposts album").first.id)
end
根据我之前提出的问题,确定我需要使用accepts_nested_attributes_for、fields_for 以便能够使用一种形式更新多个模型。这就是我设置的方式。
微贴模型:
class Micropost < ActiveRecord::Base
belongs_to :user
has_one :photo
accepts_nested_attributes_for :photo
attr_accessible :content, :user_id, :poster_id, :username, :image, :remote_image_url
end
照片模型:
class Photo < ActiveRecord::Base
belongs_to :photo_album
attr_accessible :photo_album_id, :photo_title, :image, :remote_image_url
mount_uploader :image, ImageUploader
end
最后是微博表格:
= form_for @micropost, :remote => true do |f|
= f.fields_for @photo do |p|
= p.file_field :image
= f.hidden_field :user_id
= f.text_area :content
= f.submit "Post"
起初我得到了错误:
ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: photo):
我有点困惑,因为我认为属性是自动分配的。至少这就是我在文档中读到的内容。无论如何,我继续将 :photo 添加到 micropost 模型 attr_accessible 白名单中。
第一个错误发生了,然后我得到了这个:
ActiveRecord::AssociationTypeMismatch (Photo(#2169424320) expected, got ActiveSupport::HashWithIndifferentAccess(#2157396720)):
也许我误解了这个功能是如何工作的,但我已经通读了这个并且还查看了 3.2.3 api 文档,但不是我哪里出错了。
我真的很感激一些帮助,让这个工作。
在此先感谢,我希望这篇长篇文章不受欢迎。只是想提供所有这些信息会让人们了解我正在努力做得更好。
亲切的问候
更新: 使用 :photo_attributes 而不是照片会给我以下错误:
Started POST "/microposts" for 127.0.0.1 at 2012-05-10 21:01:11 +0100
[02b23327ad83000f75c418d8739e7f49] [127.0.0.1] Processing by MicropostsController#create as JS
[02b23327ad83000f75c418d8739e7f49] [127.0.0.1] Parameters: {"micropost"=>{"photo"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x00000102c293d8 @original_filename="7seriesbmw.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"micropost[photo][image]\"; filename=\"7seriesbmw.jpeg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/var/folders/fh/fhADKPjGG8qSuCeoHCTNYE+++TI/-Tmp-/RackMultipart20120510-14787-1e1mrhh>>}, "user_id"=>"2", "content"=>"ioo"}, "commit"=>"Post", "utf8"=>"✓", "authenticity_token"=>"/y8Lr+e7xgabt60GWxnMGvCtIi7IjqrYDoA84vAqYcE=", "remotipart_submitted"=>"true", "X-Requested-With"=>"IFrame", "X-Http-Accept"=>"text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01"}
[02b23327ad83000f75c418d8739e7f49] [127.0.0.1] User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
[02b23327ad83000f75c418d8739e7f49] [127.0.0.1] Completed 500 Internal Server Error in 649ms
[02b23327ad83000f75c418d8739e7f49] [127.0.0.1]
ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: photo):
app/controllers/microposts_controller.rb:9:in `create'
将 attr_accessor 改回 :photo 而不是 :photo_attributes 后:
Started POST "/microposts" for 127.0.0.1 at 2012-05-10 21:20:07 +0100
[985e0f204bf7ffac1f7c02fbec35ad9b] [127.0.0.1] Processing by MicropostsController#create as JS
[985e0f204bf7ffac1f7c02fbec35ad9b] [127.0.0.1] Parameters: {"micropost"=>{"photo"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x00000102f8a3b0 @original_filename="7seriesbmw.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"micropost[photo][image]\"; filename=\"7seriesbmw.png\"\r\nContent-Type: image/png\r\n", @tempfile=#<File:/var/folders/fh/fhADKPjGG8qSuCeoHCTNYE+++TI/-Tmp-/RackMultipart20120510-15197-9rt2xn>>}, "user_id"=>"2", "content"=>"pp"}, "commit"=>"Post", "utf8"=>"✓", "authenticity_token"=>"/y8Lr+e7xgabt60GWxnMGvCtIi7IjqrYDoA84vAqYcE=", "remotipart_submitted"=>"true", "X-Requested-With"=>"IFrame", "X-Http-Accept"=>"text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01"}
[985e0f204bf7ffac1f7c02fbec35ad9b] [127.0.0.1] User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
[985e0f204bf7ffac1f7c02fbec35ad9b] [127.0.0.1] Completed 500 Internal Server Error in 452ms
[985e0f204bf7ffac1f7c02fbec35ad9b] [127.0.0.1]
ActiveRecord::AssociationTypeMismatch (Photo(#2180069640) expected, got ActiveSupport::HashWithIndifferentAccess(#2153916820)):
app/controllers/microposts_controller.rb:9:in `create'
Microposts 控制器创建动作:
def create
if params[:micropost][:user_id].to_i == current_user.id
@micropost = current_user.microposts.build(params[:micropost])
@comment = Comment.new(:user_id => current_user.id)
respond_to do |format|
if @micropost.save
format.js { render :post_on_springboard }
end
end
else
user = User.find_by_username(params[:micropost][:username])
@micropost = user.microposts.build(params[:micropost])
if @micropost.save
UserMailer.new_wall_post_notification(user, current_user).deliver if user.email_notification == 1
flash[:success] = "Micropost posted"
redirect_to root_path+user.username
else
flash[:error] = "#{@micropost.errors.full_messages.first}"
redirect_to root_path+user.username
end
end
end