【问题标题】:Additional, non-Paperclip attribute ignores when updating model via accepts_nested_attributes_for通过accepts_nested_attributes_for 更新模型时会忽略附加的非Paperclip 属性
【发布时间】:2011-09-29 13:53:12
【问题描述】:

我有一个模型,“更新”,它有_许多“资产”。一个资产有has_attached_file of :asset,使用回形针。

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

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 字段中,会显示资产的默认排序顺序,但无法更新。

输入此字段的新值正在根据日志发送:

  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 复选框按预期工作。

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

【问题讨论】:

    标签: ruby-on-rails paperclip nested-attributes fields-for


    【解决方案1】:

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

    attr_accessible :sort_order
    

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

    【讨论】:

      猜你喜欢
      • 2021-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多