无法找到制造商,其中包含' =(空白)

时间:2016-07-22 15:09:39

标签: ruby-on-rails ruby ruby-on-rails-5

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

  • 制造商{id, name} - 存储汽车品牌
  • 模型{id, manufacturer_id, name} - 存储汽车模型
  • tints {id, manufacturer_id, model_id, front, sides, rear} - 存储所需色调的长度
  • 价格{id, description, price } - 存储商品的价格

我创建了一个页面来生成窗口着色的引用。该页面包含下拉菜单,供用户选择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 %>

我试图打印所选的制造商

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

1 个答案:

答案 0 :(得分:2)

在你的参数中,Find Resultsmanufacturer_id的嵌套值,而不是params散列的直接键。请尝试以下方法:

tint