我有四个MySQL表,它们应该在simple_form按钮上单击的同时填充。在表单中有表格中所有现有列的输入字段。域表没有以下其他表的ID,但是其他表的所有表中都有一个domain_id。这里是依赖关系:Rails:用于填充多个连接表的simple_form
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 %>
我的问题是如何访问视图中的其他列。除域以外的所有输入都会导致错误消息(未知方法或局部变量)。在模型中,从另一个表中访问属性非常容易,但我无法理解它在视图中的工作原理。
编辑:我现在编辑了窗体和域控制器。但是,它仍然不起作用。没有错误,但只有域表被填充。
域控制器:
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
谢谢!这次没有错误信息。但是,只有域表被填充。在我的Rails服务器控制台中也没有错误,只有创建域脚本将被执行。 – GoYoshi
您是否在DomainsController中将'params.require(:domain).permit(:whitelist_attributes => [:url_start,:url_end],::blacklist_attributes ...)''添加到'domain_params'函数中? – marmeladze
现在我已经添加了它,但仍然只有域表被填充。我必须在控制器的某处创建新的白名单/黑名单/ product_info对象吗? – GoYoshi