simple_formボタンをクリックして同時に4つのMySQLテーブルを埋めなければなりません。フォームには、テーブルの既存のすべての列の入力フィールドがあります。ドメインテーブルには、下の他のテーブルの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
ありがとうございます!今回はエラーメッセージは表示されません。ただし、ドメイン表のみが塗りつぶされます。私のレールサーバコンソールにはエラーもなく、ドメインスクリプトの作成だけが実行されます。 – GoYoshi
DomainsControllerの 'params.require(:domain).permit(:whitelist_attributes => [:url_start、:url_end]、:blacklist_attributes ...)'を 'domain_params'関数に追加しましたか? – marmeladze
これを追加しましたが、まだドメインテーブルだけがいっぱいになります。新しいホワイトリスト/ブラックリスト/ product_infoオブジェクトをコントローラのどこかに作成する必要がありますか? – GoYoshi