gpt4 book ai didi

mysql - Rails : simple_form that fills multiple, 连接表

转载 作者:行者123 更新时间:2023-11-29 11:23:34 25 4
gpt4 key购买 nike

我有四个 MySQL 表,应该通过单击 simple_form 按钮同时填充。在表单中,有表中所有现有列的输入字段。域表没有下面其他表的 id,但是其他表在其表中都有一个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存储在域的 View 中。

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

我的问题是如何访问 View 中的其他列。除了来自域的输入之外的所有输入都将导致错误消息(未知方法或局部变量)。在模型中,从另一个表访问属性非常容易,但我无法理解它在 View 中的工作原理。

编辑:我现在已经编辑了表单和域 Controller 。然而,它仍然不起作用。没有错误,但只有域表被填充。

域 Controller :

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

并在您的 Controller 操作中添加@domain.whitelists.build等。

斜体线应该是,

@domain.whitelists.build(params[:whitelists]) 等等。

关于mysql - Rails : simple_form that fills multiple, 连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38574135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com