找不到 'id'= 的制造商(空白)

Couldn't find Manufacturer with 'id'= (blank)

我正在尝试创建一个 Web 应用程序来练习我的 Ruby Rails 技能。我的数据库中有一些实体 manufacturers, models, tints, prices

我创建了一个页面来生成 window 着色的报价。该页面包含下拉菜单,让用户可以 select manufacturer, model, type of film(front), type of film(side+rear)

下面是表单的代码

<%= form_tag('/quotation/tints/generate') do %>
    <%= label :manufacturer_id, 'Manufacturer' %>
    <div class="field">
    <%= collection_select(:tint, :manufacturer_id, Manufacturer.order(:name), :id, :name, {:prompt => "Select Manufacturer"}) %> 
    </div>

    Model:
    <div class="field">
    <%= grouped_collection_select(:tint, :model_id, Manufacturer.order(:name), :models, :name, :id, :name, {:prompt => "Select Model"}) %> 
    </div>

    <%= label :price_front, 'Front Tint' %>
    <div class="field">
        <%= collection_select(:price, :price_front, Price.order(:name), :id, :name, {:prompt => "Select Front Tint"}) %> 
    </div>

    <%= label :price_rear, 'Size and Back Tint' %>
    <div class="field">
        <%= collection_select(:price, :price_rear, Price.order(:name), :id, :name, {:prompt => "Select Side & Rear Tint"}) %> 
    </div>
    <div class="form-group">
        <%= submit_tag 'Submit' %>
    </div>
<% end %>

提交表单后,应将其重定向到 /quotation/tints/generate 并显示下拉菜单中的值。但是,我收到一个错误,说 Couldn't find Manufacturer with 'id'=。导致错误的代码如下所示

def generate
  @manufacturers = Manufacturer.find(params[:manufacturer_id])
end

这是调试日志中的参数

{"utf8"=>"✓",
 "authenticity_token"=>"Pl2bXiRT0AoF4i0h1RCHDbuvaKJNZOkV5ULQHKxDQgZzBWWLJ2mH7ddb9akwgxbloxBIHoVaT3pcwoIGcRufpg==",
 "tint"=>{"manufacturer_id"=>"7", "model_id"=>"6"},
 "price"=>{"price_front"=>"1", "price_rear"=>"2"},
 "commit"=>"Submit"}

我可以看到每个下拉值的 id 都正确显示在参数列表中。但是,我无法打印 /quotation/tints/generate 处的值,也无法获取制造商或型号的名称。

这里是routes.rb:

get '/quotation/tints' => 'tints#quotation', :as => 'tints_quotation'
post '/quotation/tints/generate' => 'tints#generate', :as => 'generate_tints_quotation'

Tint.rb:

class Tint < ApplicationRecord
  has_many :manufacturers
  has_many :models
  belongs_to :manufacturer
  belongs_to :model

  validates_uniqueness_of :model_id, :scope => [:manufacturer_id]
end

Model.rb:

class Model < ApplicationRecord
  belongs_to :manufacturer, :dependent => :destroy
  validates :name, :presence => true
  validates_uniqueness_of :name, :scope => [:manufacturer_id]

  before_save :capitalize_content
end

Manufacruter.rb:

class Manufacturer < ApplicationRecord
  has_many :models, :dependent => :destroy
  validates :name, :presence => true, uniqueness: { case_sensitive: false }

  before_save :capitalize_content
end

tints.controller.rb:

def quotation
  render 'quotation'
end

def generate
  @manufacturers = Manufacturer.find(params[:manufacturer_id])
end

generate.html.erb:

<%= @manufacturers.name %>

我正在尝试打印制造商 selected

我已经尝试了多种方式来定义它,但我仍然面临同样的错误。任何帮助是极大的赞赏。

在您的参数中,manufacturer_idtint 的嵌套值,而不是参数哈希的直接键。请尝试以下操作:

def generate
  @manufacturers = Manufacturer.find(params[:tint][:manufacturer_id])
end