模型:
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
参数:
{"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)
我到底做错了什么?