2016-07-25 17 views
0

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 

答えて

1

あなたがそのように行うshoul wikiによります。ただ、あるべき@domain.whitelists.buildとなど

斜体の行を追加、など

<%= 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(params[:whitelists])とをaccepts_nested_attributes for :whitelistsを追加します。

+0

ありがとうございます!今回はエラーメッセージは表示されません。ただし、ドメイン表のみが塗りつぶされます。私のレールサーバコンソールにはエラーもなく、ドメインスクリプトの作成だけが実行されます。 – GoYoshi

+1

DomainsControllerの 'params.require(:domain).permit(:whitelist_attributes => [:url_start、:url_end]、:blacklist_attributes ...)'を 'domain_params'関数に追加しましたか? – marmeladze

+0

これを追加しましたが、まだドメインテーブルだけがいっぱいになります。新しいホワイトリスト/ブラックリスト/ product_infoオブジェクトをコントローラのどこかに作成する必要がありますか? – GoYoshi

0

解決済み!それは簡単な解決策だった:

params.require(:domain).permit(:whitelist_attributes => [:url_start, :url_end], :blacklist_attributes ...) 

すべてのオブジェクトは、ここで特異であるが、私はそれがwhitelists_attributesblacklists_attributesを複数に変化したとき...それは働きました!すべてのテーブルは今すぐ満たされます!

関連する問題