【问题标题】:Nested Attribute not Saved in Database (Rails 5.1)嵌套属性未保存在数据库中 (Rails 5.1)
【发布时间】:2018-02-02 21:14:53
【问题描述】:

我有两个模型,CountriesRegions。我正在尝试将区域设置为国家的嵌套属性。到目前为止,我的代码将国家写入数据库,但没有写入区域。我没有错误。

就关系而言,我不确定的另一件事是,用户应该添加一个嵌套了国家/地区的区域,还是反过来让用户添加一个嵌套了区域的国家/地区?

country.rb

class Country < ApplicationRecord
  has_many :regions, inverse_of: :country
  has_many :roasts
  accepts_nested_attributes_for :regions

  validates :name, presence: true
end

region.rb

class Region < ApplicationRecord
  belongs_to :country, inverse_of: :region

  validates :name, uniqueness: true
  validates :name, presence: true
end

country_controller.rb

def country_params
  params.require(:country).permit(:name, :description, regions_attributes: [:id, :name, :description])
end

国家/地区/_form.html.rb

<%= form_with(model: country, local: true) do |form| %>
  <% if country.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(country.errors.count, "error") %> prohibited this country from being saved:</h2>

      <ul>
      <% country.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= form.label :name %>
    <%= form.text_field :name, id: :country_name %>
  </div>

  <div class="field">
    <%= form.label :description %>
    <%= form.text_field :description, id: :country_description %>
  </div>
      //nested region form
      <%= form.fields_for :region do |region| %>
    <p>
        Region: <%= region.text_field :name %>
    </p>
    <% end %>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

更新

Region 不是允许的参数。检查控制器,我有它作为参数?

  Parameters: {"utf8"=>"✓", "authenticity_token"=>"wUtZvA8rMeQ12onWg+B4OcbzGzZOIDOLwi99Aef3SnjAg5yyYA0qI8wNJIl41u/S0+RIlMAvkVwWVyWWPF3Ocg==", "country"=>{"name"=>"Guatemala", "description"=>"", "region"=>{"name"=>"Candelaria"}}, "commit"=>"Create Country"}
Unpermitted parameter: :region
   (0.1ms)  BEGIN
  SQL (0.8ms)  INSERT INTO "countries" ("name", "description", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["name", "Guatemala"], ["description", ""], ["created_at", "2018-02-02 22:21:24.093876"], ["updated_at", "2018-02-02 22:21:24.093876"]]
   (0.3ms)  COMMIT
Redirected to http://localhost:3000/countries/10
Completed 302 Found in 5ms (ActiveRecord: 1.2ms)

更新 2

我现在允许区域参数,但似乎我实际上并没有发送任何指令来创建区域。因此,我补充说:

  def new
    @country = Country.new
    @country.region.build  //doesn't work
    @country.regions.build //doesn't work
    @country.build_region  //doesn't work
    @country.build_regions //doesn't work
  end

但这只会产生错误undefined method 'build' for nil:NilClass

【问题讨论】:

    标签: ruby-on-rails nested-forms


    【解决方案1】:

    我会改变:

    <%= form.fields_for :region do |region| %>
      <p>
        Region: <%= region.text_field :name %>
      </p>
    <% end %>
    

    <%= form.fields_for :regions do |region| %>
      <p>
        Region: <%= region.text_field :name %>
      </p>
    <% end %>
    

    <%= form.fields_for country.regions.build do |region| %>
      <p>
        Region: <%= region.text_field :name %>
      </p>
    <% end %>
    

    【讨论】:

    • 嘿巴勃罗。已将表单更新为regions,但根据 OP. 中的更新,我意识到我实际上并没有指示代码写入该字段。也就是说,我收到了一个新错误undefined method build
    • 好吧。 @country.regions.build 应该可以工作。 :) 我没有发现任何问题。
    【解决方案2】:

    您设置了视图和强参数以支持has_one :region associationhas_many :regions 有一些问题。你可以试试cocoon,Nested forms in rails - accessing attribute in has_many relation

    【讨论】:

    • 看起来区域不是允许的参数。把日志输出放到OP中
    • 你尝试做的和我回答的是has_one :region关联。如果你改变它,它将起作用。对于has_many :regions,您必须在强参数中以不同的方式处理country.regions.build
    • @SimonCooper 更新 has_one :region 关联的答案。
    • @SimonCooper 完全改变了答案。希望对你有帮助
    【解决方案3】:

    更新

    <%= form.fields_for :region do |region| %>
    

    <%= form.fields_for :regions do |region| %>
    

    【讨论】:

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