Melvin Ch'ng Melvin Ch'ng - 4 months ago 8
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>

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


When the form is submitted, it should be redirected to
/quotation/tints/generate
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])
end


Here is the parameter from the debug log

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


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

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


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

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])
end