Ruby on rails 嵌套属性未保存在数据库中(Rails 5.1)
我有两种型号,Ruby on rails 嵌套属性未保存在数据库中(Rails 5.1),ruby-on-rails,nested-forms,Ruby On Rails,Nested Forms,我有两种型号,国家和地区。我试图将区域设置为国家的嵌套属性。到目前为止,我的代码将国家写入数据库,但不写入地区。我没有错误 在关系方面,我不确定的另一件事是,用户应该添加一个国家嵌套的地区,还是相反,用户应该添加一个国家嵌套的地区 country.rb class Country < ApplicationRecord has_many :regions, inverse_of: :country has_many :roasts accepts_nested_attribut
国家
和地区
。我试图将区域设置为国家的嵌套属性。到目前为止,我的代码将国家写入数据库,但不写入地区。我没有错误
在关系方面,我不确定的另一件事是,用户应该添加一个国家嵌套的地区,还是相反,用户应该添加一个国家嵌套的地区
country.rb
class Country < ApplicationRecord
has_many :regions, inverse_of: :country
has_many :roasts
accepts_nested_attributes_for :regions
validates :name, presence: true
end
class Region < ApplicationRecord
belongs_to :country, inverse_of: :region
validates :name, uniqueness: true
validates :name, presence: true
end
def country_params
params.require(:country).permit(:name, :description, regions_attributes: [:id, :name, :description])
end
<%= 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 %>
country/_form.html.rb
class Country < ApplicationRecord
has_many :regions, inverse_of: :country
has_many :roasts
accepts_nested_attributes_for :regions
validates :name, presence: true
end
class Region < ApplicationRecord
belongs_to :country, inverse_of: :region
validates :name, uniqueness: true
validates :name, presence: true
end
def country_params
params.require(:country).permit(:name, :description, regions_attributes: [:id, :name, :description])
end
<%= 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 %>
更新2
我现在得到了regions参数allowed,但似乎我实际上没有发送任何创建区域的指令。因此,我补充说:
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
但这只会为nil:NilClass生成错误
未定义的方法“build”
您设置了视图&strong参数以支持has\u one:region关联<代码>有很多:区域
有一些陷阱。你可以试试,我会改变:
<%= form.fields_for :region do |region| %>
<p>
Region: <%= region.text_field :name %>
</p>
<% end %>
地区:
与
地区:
或
地区:
更新
<%= form.fields_for :region do |region| %>
到
看起来区域不是允许的参数。将日志输出放在OPWhat您尝试执行的操作中,我的回答是针对has_one:region
关联。如果你改变这一点,它就会起作用。对于has_多:region
您必须执行country.regions.build
在强参数中以不同方式手动处理。@SimonOper更新has_多:region关联的答案。@SimonOper完全更改了答案。希望它能帮助谢·巴勃罗。已将表单更新为区域
,但根据OP中的更新,我意识到我实际上没有指示代码写入该字段。也就是说,我得到了一个新的错误未定义的方法build
@country.regions.build应该可以工作。:)我看不出有什么不对劲。