型号:
class User < ActiveRecord::Base
attr_accessible :soundcloud_access_token, :soundcloud_expires_at, :soundcloud_refresh_token, :soundcloud_user_id, :soundcloud_username, :photo
has_one :photo
accepts_nested_attributes_for :photo
查看:
=form_for :user, :url => user_path(@user), :method => :put, :class => 'form-horizontal' do |f|
%legend Edit Profile
=f.fields_for :photo do |photo|
%div.control-group
=photo.label :image
%div.controls
=photo.file_field :image
=f.submit 'Submit'
控制器:
def update
@user = User.find(params[:id])
@user.update_attributes(params[:user])
redirect_to @user
end
PARAMS:
{"utf8"=>"✓", "_method"=>"put", "authenticity_token"=>"nqxi4QKr7g7j0xO41lZqCHvQrGHSd7AXqxEZyskxag8=", "user"=>{"photo"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0xb1d442a0 @original_filename="Dark-Evil-41694.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[photo][image]\"; filename=\"Dark-Evil-41694.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/tmp/RackMultipart20120908-8454-qgtagx>>}}, "commit"=>"Submit", "action"=>"update", "controller"=>"users", "id"=>"1"}
错误:
Photo(#-646360948) expected, got ActiveSupport::HashWithIndifferentAccess(#76750090)
我在这里做错了什么?
答案 0 :(得分:2)
使用
{p>:photo_attributes
而不是:photo
中的attr_accessible
attr_accessible :soundcloud_access_token, :soundcloud_expires_at, :soundcloud_refresh_token, :soundcloud_user_id, :soundcloud_username, :photo_attributes
这个(调查员Samo自己的解决方案:))
{p>:photo_attributes
而不是:photo
中的fields_for
我不需要这个步骤轨道3.2,Samo的版本问题在3.2中很好用
文档中的更多细节
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for