嵌套属性未保存在数据库中(Rails 5.1)

时间:2018-02-02 21:14:53

标签: ruby-on-rails nested-forms

我有两个模型,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

3 个答案:

答案 0 :(得分:1)

我会改变:

<%= 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 %>

@Contract

答案 1 :(得分:0)

您设置视图&amp;支持has_one :region association的强参数。 has_many :regions有一些陷阱。您可以尝试cocoonNested forms in rails - accessing attribute in has_many relation

答案 2 :(得分:0)

更新

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

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