多个select for rails中的连接表问题

问题描述:

我的应用程序中有三种模型:Customer,Product和Customerproduct。客户产品一起加入Custumer和Product。 Thsu每个模型看起来是这样的:多个select for 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 

每当我试图创建一个新的客户,一压脚提升选择产品的列表中,我得到错误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

你的控制器文件名应该是customers_controller.rbcustomer_controllers.rb和里面CustomersController < ApplicationController遵循Rails的约定。

此外,在您的customer窗体中,您使用的是局部变量,而不是从控制器传递的实例变量。在您的表单中将customer更改为@customer

+0

我试过了,仍然有同样的错误。 – Jioke