通过 Accepts_nested_attributes_for 更新模型时会忽略附加的非 Paperclip 属性

发布于 2024-12-07 08:58:28 字数 2544 浏览 1 评论 0原文

我有一个模型“更新”,其中有很多“资产”。资产具有 :asset 的 has_attached_file,使用 Paperclip。

我可以通过更新表单(使用 fields_for)成功创建多个资产,但在编辑更新时,我无法更新资产上名为“sort_order”的附加非 Paperclip 属性。新值已发布,但对象似乎没有更新。

models/asset.rb

...
belongs_to :update
...

models/update.rb

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

我没有在任一模型上使用 attr_accessible

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>

在上面的 a.text_field :sort_order 字段中,会显示资产的默认 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 复选框按预期工作,但这也是如此。

希望这是足够的信息;如果有人可以帮助我将非常感激!

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 either 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!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

所有深爱都是秘密 2024-12-14 08:58:28

我认为您必须在资产模型中使用 attr_accessible :

attr_accessible :sort_order

这应该可以解决您的问题。

I think you must use attr_accessible in your assets model:

attr_accessible :sort_order

That should solve your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文