Rails不会更新嵌套属性

时间:2016-09-13 14:42:04

标签: ruby-on-rails ruby nested-attributes

我创建了一个应用程序,它基本上是具有交互式和动态表单的RPG的角色创建者。我使用Rails 5.0.0.1,我无法正确更新我的表单。基本模型更新得很好,但所有嵌套都没有。

所以,我有

class Character < ApplicationRecord
  has_many :attr4characters, autosave: true
  accepts_nested_attributes_for :attr4characters, allow_destroy: true
end

class Attr4character < ApplicationRecord
  belongs_to :character
end

代表一个角色和一组他的属性。 Attr4character中的每条记录都是不同的属性。

Show视图很简单:

...
<div class="field">
  <%= f.label :description %>
  <%= f.text_area :description %>
</div>

<div class="field">
  <%= f.label :character_type %>
  <%= f.select(:character_type, options_for_select(["Zhong Lung", "Shih", "Hsien", "Garou", "Technocrat"])) %>
</div>

  <% f.object.attr4characters.each do |attr| %>
    <%= f.fields_for attr do |attr_f| %>
      <div class="field">  
        <%= attr_f.label "Field name" %>
        <%= attr_f.text_field :field_name %>
      </div>
      <div class="field">  
        <%= attr_f.label "Field value" %>
        <%= attr_f.text_field :field_value %>
      </div>
    <% end %>
  <% end %>

  <div class="actions">
    <%= f.submit %>
  </div>
...

最后我的characters_controller:

  def update
    respond_to do |format|
      if @character.update(character_params)
        format.html { redirect_to @character, notice: 'Character was successfully updated.' }
        format.json { render :show, status: :ok, location: @character }
      else
        format.html { render :edit }
        format.json { render json: @character.errors, status:  :unprocessable_entity }
      end
    end
  end

  private
    def set_character
      @character = Character.find(params[:id])
    end

    def character_params
      params.require(:character).permit(:id, :name, :player, :description, :character_type, attr4characters_attributes: [:id, :character_id, :field_name, :field_value])
    end

所以,我有一个表单,它正确显示一个字符和他的所有属性集。当我更新字符字段(如:description)时,它通常会更新。当我更新任何嵌套字段时,Rails说该字符已成功更新,没有任何变化!我在Google上搜索过,我在Rails表单中发现了嵌套属性的许多问题,但没有一个方法适合我。我甚至认为这是Rails 4中的一个错误,但我使用的是第5版......

请关于这个主题的建议。真的是个bug吗?或者我做错了什么?我是Rails的新手,所以我不排除这种可能性。 :)

顺便说一下,在服务器的日志中,我发现有关于attr4字符的警告。

Processing by CharactersController#update as HTML
  Parameters: {"utf8"=>"\u2713", "authenticity_token"=>"xeYyIRc13YiOk29v18rFM6Oh5OHRRuPpSKEQuIHE/U4uhANEF7TwMp8mb6hv6L7mUAm5MngAuyFayHcWV/Vvbw==", "character"=>{"name"=>"Ray", "player"=>"111", "description"=>"A Zhong Lung suicider", "character_type"=>"Hsien", "attr4character"=>{"field_name"=>"Gift1", "field_value"=>"Sense Wyrm"}}, "commit"=>"Update Character", "id"=>"3"}
  Character Load (0.2ms)  SELECT  "characters".* FROM "characters" WHERE "characters"."id" = ? LIMIT ?  [["id", 3], ["LIMIT", 1]]
Unpermitted parameter: attr4character
   (0.1ms)  begin transaction
  SQL (0.9ms)  UPDATE "characters" SET "character_type" = ?, "updated_at" = ? WHERE "characters"."id" = ?  [["character_type", "Hsien"], ["updated_at", 2016-09-13 14:39:10 UTC], ["id", 3]]
   (24.3ms)  commit transaction

但是在characters_controller ...

中允许使用attr4字符

1 个答案:

答案 0 :(得分:1)

警告告诉您它忽略了所有attr4character属性。您的permit代码与您的型号一样正确,但您的视图与它们不匹配。你应该做的

f.fields_for :attr4characters do |attr_f|
  ...
end

让rails处理迭代关联。这也将确保正确命名属性(因此白名单允许它们通过)