开发者

Additional, non-Paperclip attribute ignores when updating model via accepts_nested_attributes_for

开发者 https://www.devze.com 2023-04-09 02:39 出处:网络
I have a model, \"Update\" which has_many \"Assets\". An Asset has has_attached_file of :asset, using Paperclip.

I have a model, "Update" which has_many "Assets". An Asset has has_attached_file of :asset, using Paperclip.

I can successfully create multiple assets through my update form (using fields_for), but when editing an Update, I can't update an additional, non Paperclip attribute called "sort_order" on the asset. The new values are posted, but the object doesn't seem to be updated.

models/asset.rb

...
belongs_to :update
...

models/update.rb

has_many :assets, :dependent => :destroy
...
accepts_nested_attributes_for :assets, :allow_destroy => true

I am not using attr_accessible on eithe开发者_如何学运维r model.

views/updates/_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>

In the a.text_field :sort_order field above, the default sort_order for the asset appears, but cannot be updated.

New values entered into this field are being sent as per the log:

  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" is the new value) yet the value isn't being saved.

This is also despite the _destroy checkbox working as expected.

Hope that's enough information; if anyone can help I'd be so grateful!!


I think you must use attr_accessible in your assets model:

attr_accessible :sort_order

That should solve your problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号