2017-04-04 2 views
0

私のアプリケーションには、Customer、Product、Customerproductという3つのモデルがあります。 CustomerProductはCustumerとProductを結合します。 Thsuモデルのそれぞれは、次のようになります。Railsの複数選択での結合テーブルに関する問題

class Customer < ApplicationRecord 

    has_many :customerproducts 
    has_many :products, through: :customerproducts 
end 


class Customerproduct < ApplicationRecord 
    belongs_to :customer 
    belongs_to :product 
end 

class Product < ApplicationRecord 
    has_many :customerproducts 
    has_many :customers, through: :customerproducts 
end 

customers_controllers.rbの一部は次のようになります。私は新しい顧客を作成しようとするたびに

def new 
    @customer = Customer.new 
    get_products 
    end 

    # POST /customers 
    # POST /customers.json 
    def create 
    @customer = Customer.new(customer_params) 
    get_products 

    params[:products][:id].each do |product| 
     if !product.blank? 
     @customer.customerproducts.build(:product_id => product) 
     end 
    end 

    respond_to do |format| 
     if @customer.save 
     format.html { redirect_to @customer, notice: 'Customer was successfully created.' } 
     format.json { render :show, status: :created, location: @customer } 
     else 
     format.html { render :new } 
     format.json { render json: @customer.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_customer 
     @customer = Customer.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def customer_params 
     params.require(:customer).permit(:name, :address, :email, :phone_number, :occupation, :field, :country, :customertype_id, :status, :sales_cycle, :sub_id) 
    end 

    def get_products 
     @all_products = Product.all 
     @customer_product = @customer.customerproducts.build 
    end 

、製品のリストを選択するFTERは、私を得ますエラーCustomerproducts is invalid。私は何が間違っている可能性があります。これまで私は考えているすべてのソリューションを試してきましたが、何も働いていませんでした。 Customerproducts is invalid

これは私のフォームのビューです(この質問に関係のない部分は削除されています)。

<%= form_for(customer) do |f| %> 
    <% if customer.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(customer.errors.count, "error") %> prohibited this customer from being saved:</h2> 

     <ul> 
     <% customer.errors.full_messages.each do |message| %> 
     <li><%= message %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <div> 
    <%= f.label :customertype %> 
    <%= f.select(:customertype_id, Customertype.all.map { |p| [ p.title, p.id ] }, { include_blank: 'Select Customer Type'}) %> 
    </div> 

    <%= fields_for(@customer_product) do |ff| %> 
    <div class="field"> 
     <%= f.label "Select Products" %> 
     <%= collection_select(:products, :id, @all_products, :id, :name, { include_blank: false, include_hidden: false }, { multiple: true }) %> 
    </div> 
    <% end %> 

    <div> 
    <%= f.label :sub %> 
    <%= f.select(:sub_id, Sub.all.map { |p| [ p.title, p.id ] }, { include_blank: 'Select SUB'}) %> 
    </div> 

    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

Serverのログ

Started GET "/customers/new" for 127.0.0.1 at 2017-04-04 23:48:51 +0100 
Processing by CustomersController#new as HTML 
    Rendering customers/new.html.erb within layouts/application 
    Customertype Load (145.3ms) SELECT "customertypes".* FROM "customertypes" 
    Product Load (1.0ms) SELECT "products".* FROM "products" 
    Sub Load (1.7ms) SELECT "subs".* FROM "subs" 
    Rendered customers/_form.html.erb (339.8ms) 
    Rendered customers/new.html.erb within layouts/application (487.6ms) 
Completed 200 OK in 1746ms (Views: 1414.8ms | ActiveRecord: 148.0ms) 


Started POST "/customers" for 127.0.0.1 at 2017-04-04 23:49:22 +0100 
Processing by CustomersController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"OKDBbqi24/ZNxcfhYwRghwfpCh/XHFYjxGbq8XPxRmSzc+uB7XrrfG9RkiZ/8FTbHZfnaRlK3SCvIG88/nfbVg==", "customer"=>{"customertype_id"=>"1", "name"=>"lord henry", "address"=>"9 Olusegun Street", "country"=>"NG", "email"=>"[email protected]", "phone_number"=>"08122334455", "occupation"=>"Engineer", "field"=>"Civil", "sub_id"=>"2"}, "products"=>{"id"=>["1", "3"]}, "commit"=>"Create Customer"} 
    (275.6ms) BEGIN 
    Customertype Load (1.6ms) SELECT "customertypes".* FROM "customertypes" WHERE "customertypes"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]] 
    Sub Load (1.8ms) SELECT "subs".* FROM "subs" WHERE "subs"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]] 
    Product Load (1.1ms) SELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]] 
    Product Load (2.6ms) SELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2 [["id", 3], ["LIMIT", 1]] 
    (1.2ms) ROLLBACK 
    Rendering customers/new.html.erb within layouts/application 
    Customertype Load (1.4ms) SELECT "customertypes".* FROM "customertypes" 
    Product Load (1.1ms) SELECT "products".* FROM "products" 
    Sub Load (2.0ms) SELECT "subs".* FROM "subs" 
    Rendered customers/_form.html.erb (313.0ms) 
    Rendered customers/new.html.erb within layouts/application (322.4ms) 
Completed 200 OK in 1351ms (Views: 873.4ms | ActiveRecord: 299.0ms) 
+0

完全なエラーメッセージを投稿してください –

+0

@MartinZinovskyエラーメッセージに画像を含めました。 「顧客製品は無効です」と表示されます。 – Jioke

+0

あなたもサーバーログを投稿できますか? – amrrbakry

答えて

0

お使いのコントローラのファイル名はcustomers_controller.rbないcustomer_controllers.rbCustomersController < ApplicationControllerその中にはRailsの規則に従うことをする必要があります。

また、customerフォームでは、コントローラから渡されたインスタンス変数の代わりにローカル変数を使用しています。フォーム内でcustomer@customerに変更してください。

+0

私はそれを試みたが、同じエラーがまだあった。 – Jioke

関連する問題