通过 Accepts_nested_attributes_for 更新模型时会忽略附加的非 Paperclip 属性
我有一个模型“更新”,其中有很多“资产”。资产具有 :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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您必须在资产模型中使用 attr_accessible :
这应该可以解决您的问题。
I think you must use attr_accessible in your assets model:
That should solve your problem.