2011-09-29 80 views
2

我有一個模型,「更新」has_many「資產」。資產具有has_attached_file:資產,使用回形針。另外,非回形針屬性忽略更新模型時通過accept_nested_attributes_for

我可以通過我的更新表單(使用fields_for)成功創建多個資產,但在編輯更新時,我無法更新資產上名爲「sort_order」的附加非Paperclip屬性。新值已發佈,但該對象似乎沒有更新。

型號/ asset.rb

... 
belongs_to :update 
... 

型號/ update.rb

has_many :assets, :dependent => :destroy 
... 
accepts_nested_attributes_for :assets, :allow_destroy => true 
我不是在任模型中使用 attr_accessible

的意見/更新/ _form.html.erb

<ul class="existing-images"> 
    <%= f.fields_for :assets do |a| %> 
     <% unless a.object.new_record? %> 
      <li> 
       <%= link_to image_tag(a.object.asset.url(:small)), a.object.asset.url(:original) %> 

       <%= a.check_box :_destroy %> 
       <%= a.label :_destroy %> 

       <%= a.text_field :sort_order %> 
       <%= a.label :sort_order %> 
      </li> 
     <% end %> 
    <% end %> 
    </ul> 

在上面的a.text_field :sort_order領域,出現了資產的默認排序順序,但不能更新。

輸入到該字段的新值被髮送作爲每日誌:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"2IUei4WR7fRpsM0TKD3Yk8u5FlYv2FDszzjJc3y4eG8=", "update"=>{"year"=>"2011", "week"=>"39", "title"=>"A new piece of work", "content"=>"", "assets_attributes"=>{"3"=>{"_destroy"=>"0", "sort_order"=>"1", "id"=>"1"}, "4"=>{"_destroy"=>"0", **"sort_order"=>"20"**, "id"=>"2"}}, "video_url"=>"", "quote"=>"", "allow_reactions"=>"1", "is_published"=>"1", "allow_public_feed"=>"0"}, "id"=>"1"} 
    User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 
    Update Load (0.2ms) SELECT "updates".* FROM "updates" WHERE "updates"."id" = ? LIMIT 1 [["id", "1"]] 
    Asset Load (0.4ms) SELECT "assets".* FROM "assets" WHERE "assets"."update_id" = 1 AND "assets"."id" IN (1, 2) ORDER BY assets.sort_order 
    (0.1ms) SELECT 1 FROM "updates" WHERE ("updates"."update_type_id" = 1 AND "updates"."id" != 1 AND "updates"."year" = 2011 AND "updates"."week" = 39 AND "updates"."user_id" = 1) LIMIT 1 
    Update Load (0.1ms) SELECT "updates".* FROM "updates" WHERE "updates"."id" = 1 LIMIT 1 
[paperclip] Saving attachments. 

(「SORT_ORDER」 =>「20」是新的值),但該值不被保存。

儘管_destroy複選框按預期工作,也是如此。

希望這是足夠的信息;如果任何人都可以幫助,我會很感激!

回答

1

我想你一定在你的資產模型中使用attr_accessible:

attr_accessible :sort_order 

這應該解決您的問題。