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

时间:2017-04-04 22:18:29

标签: ruby-on-rails model-associations multipleselection

我的应用程序中有三个模型:客户,产品和客户产品。 Customerproduct将Custumer和Product连接在一起。每个模型都是这样的:

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 %>

服务器日志

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"=>"me@gmail.com", "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)

1 个答案:

答案 0 :(得分:0)

您的控制器文件名应为customers_controller.rb而不是customer_controllers.rb,并且在CustomersController < ApplicationController内,以遵循Rails约定。

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