Rails:simple_form 填充多个连接的表
Rails: simple_form that fills multiple, connected tables
我有四个 MySQL table 应该通过单击 simple_form 按钮同时填充。在表单中,有来自 table 的所有现有列的输入字段。域 table 没有下面其他 table 的 ID,但是其他人的 table 中都有一个 domain_id。以下是依赖项:
class Domain < ActiveRecord::Base
has_many :whitelists
has_many :blacklists
has_many :product_infos
end
class Whitelist < ActiveRecord::Base
belongs_to :domain
end
class Blacklists < ActiveRecord::Base
belongs_to :domain
end
class ProductInfo < ActiveRecord::Base
belongs_to :domain
end
我的simple_form存储在域的视图中。
<%= simple_form_for @domain do |f| %>
<%= f.input :name, placeholder: 'Example Shop' %>
<%= f.input :domain, placeholder: 'http://www.example.com' %>
<h3><b>Whitelist</b></h3>
<%= f.input :url_start, as: :text %>
<%= f.input :url_end, as: :text %><br>
<h3><b>Blacklist</b></h3>
<%= f.input :url_start, as: :text %>
<%= f.input :url_end, as: :text %><br>
<h3><b>Product Information</b></h3>
<%= f.input :id_tag %>
<%= f.input :name_tag %>
<%= f.input :product_info_text_tag %><br>
<%= f.button :submit %>
<% end %>
我的问题是如何访问视图中的其他列。除了来自域的输入之外的所有输入都将导致错误消息(未知方法或局部变量)。在模型中,从另一个 table 访问属性非常容易,但我无法理解它在视图中的工作原理。
编辑: 我现在已经编辑了表单和域控制器。但是,它仍然不起作用。没有错误,但只有域 table 被填充。
域控制器:
def new
@domain = Domain.new
@domain.whitelists.build
@domain.blacklists.build
@domain.product_infos.build
end
private
# Use callbacks to share common setup or constraints between actions.
def set_domain
@domain = Domain.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def domain_params
params.require(:domain).permit(:whitelist_attributes => [:url_start, :url_end], :blacklist_attributes => [:url_start, :url_end], :product_info_attributes => [:id_tag, :name_tag, :promo_price_tag, :price_tag, :shipping_cost_tag, :image_url_tag, :browse_tree_tag, :product_info_text_tag])
end
根据 wiki 你应该这样做。只需添加 accepts_nested_attributes for :whitelists
等
<%= simple_form_for @domain do |f| %>
<%= f.input :name, placeholder: 'Example Shop' %>
<%= f.input :domain, placeholder: 'http://www.example.com' %>
<%= f.simple_fields_for :whitelists do |w| %>
<%= w.input :url_start, as: :text %>
<%= w.input :url_end, as: :text %>
<% end %>
<%= f.simple_fields_for :blacklists do |b| %>
<%= b.input :url_start, as: :text %>
<%= b.input :url_end, as: :text %>
<% end %>
<%= f.simple_fields_for :products do |p| %>
<%= p.input :id_tag %>
<%= p.input :name_tag %>
<%= p.input :product_info_text_tag %>
<% end %>
<%= f.button :submit %>
<% end %>
并在您的控制器操作中添加 @domain.whitelists.build
等
斜线应该是,
@domain.whitelists.build(params[:whitelists])
等等。
解决了!这是一个简单的解决方案:
params.require(:domain).permit(:whitelist_attributes => [:url_start, :url_end], :blacklist_attributes ...)
这里的每个对象都是单数,但是当我将其更改为复数时 whitelists_attributes
、blacklists_attributes
... 它成功了!现在每个 table 都被填满了!
我有四个 MySQL table 应该通过单击 simple_form 按钮同时填充。在表单中,有来自 table 的所有现有列的输入字段。域 table 没有下面其他 table 的 ID,但是其他人的 table 中都有一个 domain_id。以下是依赖项:
class Domain < ActiveRecord::Base
has_many :whitelists
has_many :blacklists
has_many :product_infos
end
class Whitelist < ActiveRecord::Base
belongs_to :domain
end
class Blacklists < ActiveRecord::Base
belongs_to :domain
end
class ProductInfo < ActiveRecord::Base
belongs_to :domain
end
我的simple_form存储在域的视图中。
<%= simple_form_for @domain do |f| %>
<%= f.input :name, placeholder: 'Example Shop' %>
<%= f.input :domain, placeholder: 'http://www.example.com' %>
<h3><b>Whitelist</b></h3>
<%= f.input :url_start, as: :text %>
<%= f.input :url_end, as: :text %><br>
<h3><b>Blacklist</b></h3>
<%= f.input :url_start, as: :text %>
<%= f.input :url_end, as: :text %><br>
<h3><b>Product Information</b></h3>
<%= f.input :id_tag %>
<%= f.input :name_tag %>
<%= f.input :product_info_text_tag %><br>
<%= f.button :submit %>
<% end %>
我的问题是如何访问视图中的其他列。除了来自域的输入之外的所有输入都将导致错误消息(未知方法或局部变量)。在模型中,从另一个 table 访问属性非常容易,但我无法理解它在视图中的工作原理。
编辑: 我现在已经编辑了表单和域控制器。但是,它仍然不起作用。没有错误,但只有域 table 被填充。
域控制器:
def new
@domain = Domain.new
@domain.whitelists.build
@domain.blacklists.build
@domain.product_infos.build
end
private
# Use callbacks to share common setup or constraints between actions.
def set_domain
@domain = Domain.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def domain_params
params.require(:domain).permit(:whitelist_attributes => [:url_start, :url_end], :blacklist_attributes => [:url_start, :url_end], :product_info_attributes => [:id_tag, :name_tag, :promo_price_tag, :price_tag, :shipping_cost_tag, :image_url_tag, :browse_tree_tag, :product_info_text_tag])
end
根据 wiki 你应该这样做。只需添加 accepts_nested_attributes for :whitelists
等
<%= simple_form_for @domain do |f| %>
<%= f.input :name, placeholder: 'Example Shop' %>
<%= f.input :domain, placeholder: 'http://www.example.com' %>
<%= f.simple_fields_for :whitelists do |w| %>
<%= w.input :url_start, as: :text %>
<%= w.input :url_end, as: :text %>
<% end %>
<%= f.simple_fields_for :blacklists do |b| %>
<%= b.input :url_start, as: :text %>
<%= b.input :url_end, as: :text %>
<% end %>
<%= f.simple_fields_for :products do |p| %>
<%= p.input :id_tag %>
<%= p.input :name_tag %>
<%= p.input :product_info_text_tag %>
<% end %>
<%= f.button :submit %>
<% end %>
并在您的控制器操作中添加 @domain.whitelists.build
等
斜线应该是,
@domain.whitelists.build(params[:whitelists])
等等。
解决了!这是一个简单的解决方案:
params.require(:domain).permit(:whitelist_attributes => [:url_start, :url_end], :blacklist_attributes ...)
这里的每个对象都是单数,但是当我将其更改为复数时 whitelists_attributes
、blacklists_attributes
... 它成功了!现在每个 table 都被填满了!