Melvin Ch'ng Melvin Ch'ng - 1 year ago 52
Ruby Question

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

I am trying to create a web application to practise my Ruby on Rails skill. I have a few entities in my database

manufacturers, models, tints, prices

  • manufacturers
    {id, name}
    - stores the make of the car

  • models
    {id, manufacturer_id, name}
    - stores the models of the car

  • tints
    {id, manufacturer_id, model_id, front, sides, rear}
    - stores the length of tint required

  • prices
    {id, description, price }
    - stores the price of the item

I created a page to generate a quotation for window tinting. The page includes drop-down menus to let user to select
manufacturer, model, type of film(front), type of film(side+rear)

Below is the code for the form

<%= 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 class="field">
<%= grouped_collection_select(:tint, :model_id, Manufacturer.order(:name), :models, :name, :id, :name, {:prompt => "Select Model"}) %>

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

<%= 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 class="form-group">
<%= submit_tag 'Submit' %>
<% end %>

When the form is submitted, it should be redirected to
and display the value from the dropdown menu. However, I received an error, saying that
Couldn't find Manufacturer with 'id'=
. The code that caused the error is shown below

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

Here is the parameter from the debug log

"tint"=>{"manufacturer_id"=>"7", "model_id"=>"6"},
"price"=>{"price_front"=>"1", "price_rear"=>"2"},

I can see that the
of each drop down value are shown up correctly in the parameter list. However, I coundn't able to print the value at
nor get the name of the manufacturer or model.

Here is

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


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

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


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

before_save :capitalize_content


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

before_save :capitalize_content


def quotation
render 'quotation'

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


<%= %>

I'm trying to print the manufacturer selected

I have tried multiple ways to define it, but I am still facing the same error. Any help is greatly appreciated.

Answer Source

In your params, manufacturer_id is a nested value of tint, as opposed to being a direct key of the params hash. Try the following:

def generate
  @manufacturers = Manufacturer.find(params[:tint][:manufacturer_id])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download