0

模型:

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)

我到底做错了什么?

4

1 回答 1

2

采用

:photo_attributes而不是:photoattr_accessible

attr_accessible :soundcloud_access_token, :soundcloud_expires_at, :soundcloud_refresh_token, :soundcloud_user_id, :soundcloud_username, :photo_attributes

还有这个(发问者萨莫本人的这个解决方案:))

:photo_attributes而不是:photofields_for

我不需要这个 step rails 3.2 ,来自问题的 Samo 版本在 3.2 中效果很好

文档中的更多详细信息

http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for

http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#label-Using+with+attr_accessible

于 2012-09-08T08:56:32.247 回答