Reputation: 376
This is not a homework. I am doing this to boost my RoR skills.
I have both Manufacturer
and Model
entity. I have all the relationship setup correctly and user has to choose Manufacturer
before choosing Model
. I added more functionalities to my application that I created. Now, I would like to filter out the name
from the Model
dropdown list where the name
contains the word (Other)
.
Example for name
attribute in Model
entity: F20
, F10 (Other)
, E90
.
I would like to filter out the name of the model which contain (Other)
. SQL Statement would be Model.where.not("name LIKE ?", "%(Other)%")
. After the manufacturer
is selected, F20
and E90
will be shown. F10 (Other)
will be excluded since the name contains the word (Other)
.
This is what I have for the drop down list form.
<%= bootstrap_form_tag url: '/quotation/tints/generate' do |f| %>
<div class="field">
<%= f.collection_select :manufacturer_id, Manufacturer.order(:name), :id, :name, {:prompt => "Select Manufacturer"} %>
</div>
<div class="field">
<%= f.grouped_collection_select :model_id, Manufacturer.order(:name), :models, :name, :id, :name, {:prompt => "Select Model"} %>
</div>
//Some code here that are not related
<% end %>
What I have tried:
In my tints_controller
, this and I have updated my form in order to work with what I have modified.
def quotation
@listmanufacturer = Manufacturer.order(:name)
@listmodel = Model.where.not("name LIKE ?", "%(Other)%")
render 'quotation'
end
My form
<%= bootstrap_form_tag url: '/quotation/tints/generate' do |f| %>
<div class="field">
<%= f.collection_select :manufacturer_id, @listmanufacturer, :id, :name, {:prompt => "Select Manufacturer"} %>
</div>
<div class="field">
<%= f.grouped_collection_select :model_id, @listmanufacturer, @listmodel, :name, :id, :name, {:prompt => "Select Model"} %>
</div>
//Some code here that are not related
<% end %>
However, I received an error like TypeError: nil is not a symbol
. I am unable to modify my models_controller
since the drop down list is being used a few times in different view. Some views will need the models with the word (Other)
and some will not require.
@listmodel
is the part that breaks everything. By replacing :models
with @listmodel
, I am hopping to filter out the models name that contain (Other)
.
Any help is greatly appreciated!
Note: Up to this point, my application is working correctly. All relationships are setup correctly in model
and routes
are configured.
UPDATE, more logs
ActionView::Template::Error (#<ActiveRecord::Relation [#<Model id: 1, name: "Kancil Old", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, #<Model id: 2, name: "Kancil 04", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, #<Model id: 3, name: "Kembara", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, #<Model id: 4, name: "Kenari", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, #<Model id: 5, name: "Kelisa", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, #<Model id: 6, name: "Myvi 05", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, #<Model id: 7, name: "Rusa", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, #<Model id: 8, name: "Viva", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, #<Model id: 9, name: "Alza", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, #<Model id: 10, name: "Myvi Ii", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, ...]> is not a symbol nor a string):
6: </div>
7:
8: <div class="field">
9: <%= f.grouped_collection_select :model_id, @listmanufacturer, @listmodel, :name, :id, :name, {:prompt => "Select Model"} %>
10: </div>
11:
12: <div class="field">
app/views/tints/quotation.html.erb:9:in `block in _app_views_tints_quotation_html_erb__1724914643_132263140'
app/views/tints/quotation.html.erb:3:in `_app_views_tints_quotation_html_erb__1724914643_132263140'
app/controllers/tints_controller.rb:47:in `quotation'
Rendering C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout
Rendering C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
Rendered C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (9.0ms)
Rendering C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
Rendered C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (5.0ms)
Rendering C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
Rendered C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
Rendered C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (539.0ms)
DEPRECATION WARNING: #original_exception is deprecated. Use #cause instead. (called from status_code_with_paginate at C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/will_paginate-3.1.0/lib/will_paginate/railtie.rb:49)
Upvotes: 0
Views: 90
Reputation: 376
In my Manufacturer model
,
class Manufacturer < ApplicationRecord
//Something here
def listmodel
models.where.not("name LIKE ?", "%(Other)%")
end
end
In my form
,
<%= bootstrap_form_tag url: '/quotation/tints/generate' do |f| %>
<div class="field">
<%= f.collection_select :manufacturer_id, Manufacturer.all.order(:name), :id, :name, {:prompt => "Select Manufacturer"} %>
</div>
<div class="field">
<%= f.grouped_collection_select :model_id, Manufacturer.all.order(:name), :listmodel, :name, :id, :name, {:prompt => "Select Model"} %>
</div>
//More code here
<% end %>
Now in my drop down list, model with name containing (Other)
will be excluded.
Upvotes: 1