Rails属于具有accept_nested_attributes_for的多个模型

时间:2018-03-20 14:41:46

标签: ruby-on-rails ruby-on-rails-5.1

我正在为汽车贸易商建立发票系统,其中每张发票都链接到一个客户和一辆车,客户可能有很多发票,车辆也有很多发票。我已经使用了一个嵌套模型来执行以下操作:

purchase_invoice.rb

class PurchaseInvoice < ApplicationRecord
  belongs_to :vehicle
  accepts_nested_attributes_for :vehicle
end

vehicle.rb

class Vehicle < ApplicationRecord
  has_many :purchase_invoices
end

purchase_invoices_controller.rb

def new
  @vehicle = Vehicle.new
  @purchase_invoice = @vehicle.purchase_invoices.build
end

def create
  @vehicle = Vehicle.new
  @purchase_invoice = @vehicle.purchase_invoices.build(invoice_params)

  if @purchase_invoice.save
    redirect_to @purchase_invoice
  else
    render 'new'
  end
end

private

def invoice_params
  params.require(:purchase_invoice).permit(:buyer, :location, :vehicle_price, :transfer_fee, :balance_due, :payment_cash, :payment_bank_transfer, :payment_comment, :status, vehicle_attributes: [:vrm, :date_first_registered, :make, :model, :colour, :transmission, :vin, :fuel, :power])
end

new.html.erb

<%= form_with model: @purchase_invoice, local: true do |form| %>
  <%= form.fields_for @vehicle do |vehicle_form| %>
  <% end %>
<% end %>

然而,当我添加这样的第二个关系时:

purchase_invoice.rb

class PurchaseInvoice < ApplicationRecord
  belongs_to :customer
  belongs_to :vehicle
  accepts_nested_attributes_for :customer
  accepts_nested_attributes_for :vehicle
end

我收到错误消息:'未经许可的参数:: vehicle'

有人知道为什么吗?另外,如何在保持强对数的同时修改控制器new / create action for build?

我现在谷歌搜索这个已经有四个小时了,并尝试了很多但没有运气。提前感谢大家!

更新

这是我的日志:

Started POST "/purchase_invoices" for 127.0.0.1 at 2018-03-20 15:10:01 +0000
Processing by PurchaseInvoicesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"JB8py9zNxew6aQ6/za3JHDEb4j8f9HGujTlS6P1Eyhb+5NtPPP47fW7AHBkt9eURcnXg0gh9Mf1DCKCSwvlAbg==", "purchase_invoice"=>{"customer"=>{"name"=>""}, "vehicle"=>{"vrm"=>"SA07SSX", "make"=>"VAUXHALL", "model"=>"MERIVA DESIGN", "colour"=>"Silver", "vin"=>"W0L0XCE7574216645", "date_first_registered"=>"20/03/2007"}, "vehicle_odomoter_reading"=>"", "vehicle_number_of_keys"=>"", "vehicle_mot_expiry"=>"", "vehicle_hpi_clear"=>"", "vehicle_comments"=>"", "buyer"=>"", "location"=>"", "vehicle_price"=>"", "transfer_fee"=>"0", "balance_due"=>"", "payment_cash"=>"", "payment_bank_transfer"=>"", "payment_comments"=>""}, "commit"=>"Create Purchase invoice"}
  Vehicle Load (0.3ms)  SELECT  "vehicles".* FROM "vehicles" WHERE "vehicles"."vrm" = ? ORDER BY "vehicles"."id" ASC LIMIT ?  [["vrm", "SA07SSX"], ["LIMIT", 1]]
Unpermitted parameters: :customer, :vehicle, :payment_comments
    (0.1ms)  begin transaction
    (0.1ms)  rollback transaction
  Rendering purchase_invoices/new.html.erb within layouts/application
  Rendered purchase_invoices/new.html.erb within layouts/application (10.2ms)
  Rendered layouts/_header.html.erb (1.7ms)
Completed 200 OK in 63ms (Views: 54.4ms | ActiveRecord: 0.4ms)

1 个答案:

答案 0 :(得分:0)

你的方法有一些问题,但你非常接近。 购买发票模型属于广告系列客户,并且存储 customer_id vehicle_id 即可。这是对的。正如你在评论中所说,它不仅仅是一个连接模型,因为它包含许多其他数据,如价格,转让费等。无论如何,你传递了很多关于车辆的参数要购买,而不是车辆的 ID 。事实上,您正在 PurchaseInvoice 创建车辆,这没有任何意义。此外,您的 vehicle_attributes 不应该是数组,而是哈希(因为 PurchaseInvoice belongs_to :vehicle;因此它只是一个),并且应该只有 vehicle_id 即可。并且只需要 vehicle_id ,您就不需要nested_attributes(也不需要客户)。我会改变:

控制器:

def new
  @vehicles = Vehicle.all   #All vehicles, to select from the list
  @customers = Customer.all #All customers, to select from the list (unless you use current_user)
  @purchase_invoice = PurchaseInvoice.new
end

def create
  @vehicle = Vehicle.find(params[:vehicle_id])
  @customer = Customer.find(params[:customer_id]) # Or use current_user
  #The above is only needed to check if vehicle and customer exist, but it is not needed below

  @purchase_invoice = PurchaseInvoice.create(invoice_params)

  if @purchase_invoice.save
    redirect_to @purchase_invoice
  else
    render 'new'
  end
end

def invoice_params
  params.require(:purchase_invoice).permit(:customer_id, vehicle_id, :location, :vehicle_price, :transfer_fee, :balance_due, :payment_cash, :payment_bank_transfer, :payment_comment, :status)
end

观点:

<%= form_with model: @purchase_invoice, local: true do |form| %>

  <!-- This is necessary to choose the customer. If the customer is current_user, just remove it. -->
  <%= form.collection_select(:customer_id, @customers, :id, :name %>

  <!-- This is necessary to choose the vehicle. This is extremely simplified -->
  <!-- The customer should be able to look at many attributes of the car -->
  <!-- to be able to select one to purchase -->
  <%= form.collection_select(:vehicle_id, @vehicles, :id, :make %>

  <!-- All other invoice fields. -->
<% end %>