2017-01-30 9 views
0

こんにちは私は顧客IDを使用して1つのテーブルに顧客名を挿入します。 私のコードは顧客のみを挿入します。都市は私のコンソール フォームデータを2つのテーブルに挿入して1つのサブミットをレールに入れます

を挿入されていません

は、HTMLパラメータとして作成をPagesController番号:2017年1月30日14時40分15秒0530 処理で127.0.0.1のために、「/ページ」POST開始しました: { "UTF8" => "✓"、 "authenticity_token" => "NfyA8PpA4wZIAOPX7fYstwvt2suXoTYgl9ep1M6yTSh6ZPX8lt + oOfPX6sFXGOuxTidVND6qaksz6iZ2enGj9g =="、 "顧客" => { "名" => "fgfg"、 "custcity" => { "cityname" => "2fg"}} "commit" => "submit"}許容されないパラメータ:custcity(0.1ms)begin トランザクションSQL(2.3ms)INSERT INTO "customers"( "name"、 "created_at"、 "updated_at")VALUES(?、?、?)[["name"、 "fgfg"]、 ["created_at"、2017-01-30 09:10:15 UTC]、["updated_at "、2017-01-30 09:10:15 UTC]](231.7ms)コミットトランザクション(0.2ms)begin トランザクション(0.1ms)ロールバックトランザクション http://localhost:3000/pages完了302を247msで見つけた (ActiveRecord:234.3ms )

new.html.erb

<%= form_for @customer, url: {action: "create"} do |f| %> 
<%= label_tag("Customer") %> 
<br> 
<%= f.text_field(:name) %> 
<br> 
<%= f.fields_for @custcity do |c| %> 
     <%= label_tag("City 1") %> 
     <br> 
     <%= c.text_field(:cityname) %> 
     <br> 
     <%= label_tag("City 2") %> 
     <br> 
     <%= c.text_field(:cityname) %> 
     <br> 
<% end %> 
<br> 
<%= f.submit("submit") %> 
<% end %> 

pages_controller.rb

def create 
    @customer = Customer.new(cust_params) 
    if @customer.save 
     session[:customer_id] = @customer.id 

     #@custcity = Custcity.create(cityname: params[:customer][:cityname], cust_id: session[:customer_id]) 
     @custcity = Custcity.create({cityname: params[:customer][:cityname], cust_id: @customer.id}) 
     #@custcity.save 
     redirect_to pages_path 
    else 
     redirect_to new_page_path 
    end 
    end 

    private 
    def cust_params 
    params.require(:customer).permit(:name, :custcity => []) 
    end 
end 
+0

あなたは強いネストされたパラメータを使用して、強力なパラメータで顧客の都市のparamsを定義する必要があります:あなたはあまりにも:custcity

@custcity = Custcity.create({cityname: params[:customer][:custcity][:cityname], cust_id: @customer.id}) 

許可citynameを失ってしまいました。 –

答えて

1

:citynameへのパスが間違っています。

params.require(:customer).permit(:name, :custcity => [:cityname]) 
+0

これはエラー 'unknown attribute custcity'を返します –

+0

考え方では、CustomerとCustcityモデルはhas_many関連でリンクされている必要があり、' Custcity.create'を手動で呼び出す必要はありません。 –

関連する問題