Refactoring of many if statements for params[...], inside controller action - ruby-on-rails

I have such code, for making chain selects in my form
View for index action:
<%= form_tag do %>
<%= collection_select(*#brands_select_params) %>
<%= collection_select(*#car_models_select_params) %>
<%= collection_select(*#production_years_select_params) %>
<% # Пока еще никто ничего не выбрал %>
<%= submit_tag "Send", :id => "submit", :name => "submit" %>
And my controller:
class SearchController < ApplicationController
def index
#brands = Brand.all
#car_models = CarModel.all
if (params[:brand].blank?)
#brands_select_params = [:brand, :id, #brands, :id, :name, :prompt => "Выбирай брэнд"]
if params[:car_model].blank?
#car_models_select_params = [:car_model, :id, #car_models, :id, :name, { :prompt => "Model" }, \
{ :disabled => "disabled" }]
#production_years_select_params = [:production_year, :id, #car_models, :id, :name, { :prompt => "Year" }, \
{ :disabled => "disabled" }]
end
else
#brands_select_params = [:brand, :id, #brands, :id, :name, { :selected => params[:brand][:id] } ]
if params[:car_model].blank?
#car_models_select_params = [:car_model, :id, Brand.find(params[:brand][:id]).car_models, :id, :name, \
{ :prompt => "And model now" } ]
#production_years_select_params = [:production_year, :id, #car_models, :id, :name, { :prompt => "Year" }, \
{ :disabled => "disabled" } ]
else
#car_models_select_params = [:car_model, :id, Brand.find(params[:brand][:id]).car_models, :id, :name, \
{ :selected => params[:car_model][:id] } ] unless params[:car_model][:id].empty?
#production_years_select_params = [:production_year, :id, CarModel.find(params[:car_model][:id]).production_years, :id, :year, \
{ :prompt => "And year now" } ] unless params[:car_model][:id].empty?
end
end
end
end
As you can see, too many ifs in my controller code. And i gonna add more conditions there. After that anyone who read that code will get brain corruption. So i just wanna make it in real Ruby way, but don't know how. Please, help, guys. How should i refactor this bunch of crap?

I think a big part of the problem is you're doing too much in your controller. Generating markup (and IMO that includes building parameter lists for form helpers) should be done in views and view helpers. So:
module SearchHelper
def brand_select brands, options={}
collection_select :brand, :id, brands, :id, :name, :options
end
def car_model_select car_models, options={}
collection_select :car_model, :id, car_models, :id, :name, options
end
def production_year_select years, options={}
collection_select :production_year, :id, years, :id, :year, options
end
end
Then you can cut your controller down to this:
def index
#brands = Brand.all
#car_models = CarModel.all
#selected_brand_id = params[:brand] && params[:brand][:id]
#selected_car_model_id = params[:car_model] && params[:car_model][:id]
#production_years = #selected_car_model_id ?
[] : CarModel.find(#selected_car_model_id).production_years
end
And in your view:
<%= brand_select #brands, :prompt => "Выбирай брэнд",
:selected => #selected_brand_id
%>
<%= car_model_select #car_models, :prompt => "Model",
:selected => #selected_car_model_id
%>
<%= production_year_select #production_years, :prompt => "Year",
:selected => #selected_car_id
%>
I suspect you could simplify this even more using form_for and fields_for and get rid of the helpers entirely, but it depends a bit on how your model associations are set up.

There is no obvious solution to this kind of problem.
Generally, I try to keep the if / else architecture very clear and export all code into separate methods. 2 advantages here:
readability
easier unit testing
For your case, it would be:
class SearchController < ApplicationController
def index
#brands = Brand.all
#car_models = CarModel.all
if (params[:brand].blank?)
#brands_select_params = [:brand, :id, #brands, :id, :name, :prompt => "Выбирай брэнд"]
if params[:car_model].blank?
#car_models_select_params, #production_years_select_params = get_card_model(#car_models)
end
else
#brands_select_params = [:brand, :id, #brands, :id, :name, { :selected => params[:brand][:id] } ]
if params[:car_model].blank?
#car_models_select_params, #production_years_select_params = foo_method(#car_models)
else
#car_models_select_params, #production_years_select_params = bar_method
end
end
end
def get_card_model car_models
[
[:car_model, :id, car_models, :id, :name, { :prompt => "Model" }, { :disabled => "disabled" }],
[:production_year, :id, car_models, :id, :name, { :prompt => "Year" }, { :disabled => "disabled" }]
]
end
end

Related

how to allow multiple selections in a form.select map rails [duplicate]

I have the following select box in my form:
Related Type: <%= f.select(:TYPE, [['Type A', 'Type A'],
['Type B', 'Type B'],
['Type C', 'Type C'],
['Type D', 'Type D'],
['Type E', 'Type E']
],{ :prompt => "Please select"}
) %>
I want to allow the user to make multiple selections and also make the size of the select box 5.
How to do that for the code above?
After your { :prompt => "Please select"} add another hash with html options e.g.
<%= f.select(:TYPE, [['Type A', 'Type A'],
['Type B', 'Type B'],
['Type C', 'Type C'],
['Type D', 'Type D'],
['Type E', 'Type E']
],{ :prompt => "Please select"},
{ :multiple => true, :size => 5 }
) %>
Once you've done this you might want to move your :prompt option (keep the empty {} though so that html attributes don't get treated as Rails options.)
Also you'll need to ensure your controller code is correctly accepting and handling multiple values.
In case of collection, try
<%= f.select(:TYPE, Categories.collect {|p| [ p.name, p.id ] },
{ :prompt => "Please select"},
{ :multiple => true, :size => 5 }) %>
I have a fully working example (including preselection when editing the object), when:
Object is the considered object
similar_ids is the key to relations, and is a string
In the form:
form_for(#object) do |f|
= f.select :similar_ids, options_from_collection_for_select(Object.all, :id, :name, {:selected => #object.similar_ids.split(';')}), {}, {:multiple => true, :size => 4, :name => 'object[similar_ids][]'}
And in the Object.rb model:
class Object < ActiveRecord::Base
before_save :handle_similars
def handle_similars
self.similar_ids = self.similar_ids.select(&:present?).join(';')
# .select(&:present?) is necessary to avoid empty objects to be stored
end
def similars
self.class.find(self.similar_ids.split(';'))
end
end
These posts helped me out:
Select tag with multiple values pre-selected - Values inserted manually in database
Ruby on Rails: Submitting an array in a form
Hope it helps
HTML
<%= form.select(:product_ids, Product.all.collect {|p| [ p.name, p.id ] },
{ :prompt => "Please select"},
{ :multiple => true, :size => 5 }) %>
Controller
#category = Category.new(category_params)
def category_params
params.require(:category).permit(:name, product_ids: [])
end
{ :prompt => "Please select"}, { :multiple => true, :size => 5 } {} is important when f.select
with bootstrap selectpicker and pre selected values:
= simple_form_for [:backend, #user], html: { autocomplete: 'off' } do |f|
= f.select :role_ids, options_for_select(Role.all.map{|role| [role.name, role.id]}, #user.role_ids), {}, {:multiple => true, inlcude_blank: false, class: "form-control input-sm selectpicker"}
in controller:
def user_params
params.require(:user).permit(:id, role_ids: [])
end
# only if you havent build in new action
def new
# set user
#user.roles.any?
end
Multiple select:
= form_with url: ui_dashboard_diagrams_path, method: :get, local: true do |f|
.row
.col.form-group
= f.select :our_organization_ids,
options_for_select(OurOrganization.pluck(:name, :id), params[:our_organization_ids]),
{ include_blank: '' },
{ multiple: true, class: 'form-control form-select-multiple' }
javascript:
$(document).ready(function() {
$('.form-select-multiple').select2({
allowClear: true,
multiple: true
});
});
Add permit array our_organization_ids to your controller:
private
def diagrams_params
params.permit([our_organization_ids: []])
end
<%= f.select :tag_ids, Tag.all.collect {|t| [t.name, t.id]}, { :prompt => "Please select"}, { :multiple => true, :size => 5 } %>

Rails undefined method `map' for nil:NilClass on collection_select - The reference called may be nil

After reading through the other SO answers, it's pretty clear that there are some common themes.
Mostly this type of error seems to happen when the object called is not defined yet, but in this case we have a has_many relationship that may not have a referenced entry when building the selection.
Class Tag
property :name, type: String
has_many :in, :tagged, type: :CONCEPTUAL_TAG, model_class: :Artefact
end
One option is to do something like this:
<div class="field">
<%= f.label :tagged_id %><br>
<% if !#tag.tagged.empty? %>
<%= f.collection_select(:tagged, Artefact.all.sort { |a,b| a.name <=> b.name }, :id, :name, options = {:prompt => "Please Select an Item", :selected => #tag.tagged.map(&:id)}, html_options = {:multiple => true, :class=>"search"}) %>
<% else %>
<%= f.collection_select(:tagged, Artefact.all.sort { |a,b| a.name <=> b.name }, :id, :name, options = {:prompt => "Please Select an Item"}, html_options = {:multiple => true, :class=>"search"}) %>
<% end %>
</div>
But this is definitely not DRY.
Is there a way to select nothing when there is no association, and pre-populate when there is while keeping to a single f.collection_select?
You don't have to use the condition in your view. Even if there are no items in #tag.tagged, it is still an ActiveRecord::Relation instance, so you can go ahead and just call map on it:
<%= f.collection_select(:tagged,
Artefact.all.sort { |a,b| a.name <=> b.name },
:id,
:name,
options = {
:prompt => "Please Select an Item",
:selected => #tag.tagged.map(&:id)
},
html_options = {
:multiple => true,
:class=>"search"
})
%>

Why do I get the error `undefined method 'map'`?

In my Ruby on Rails application I am trying to display a three drop down menus in the _form.html.erb which are rendered from the file _booking_lookup.html.erb and get there data from the drop down menu methods in the models.
_form.html.erb:
<%= render(:partial => '/booking_lookup', :locals=> {:film => #film = Film.all, :showings => #showings = Showing.all, :seats => #seats = Seat.all, :my_path => '/films/booking_lookup' }) %>
_booking_lookup.html.erb:
<%= form_tag my_path, :method=>'post', :multipart => true do %>
<%= select_tag ('title_id'),
options_from_collection_for_select(#films, :id, :title_info, 0 ),
:prompt => "Film" %>
<%= select_tag ('showings_id'),
options_from_collection_for_select(#showings, :id, :showing_times, 0 ),
:prompt => "Showings" %>
<%= select_tag ('seat_id'),
options_from_collection_for_select(#seats, :id, :seats_available, 0 ),
:prompt => "Seats" %>
<%= submit_tag 'Search' %>
film.rb:
class Film < ActiveRecord::Base
has_many :showings
belongs_to :certificate
belongs_to :category
def title_info
"#{title}"
end
end
seat.rb:
class Seat < ActiveRecord::Base
belongs_to :screen
has_many :bookings
def seats_available
"#{row_letter}#{row_number}"
end
end
showing.rb:
class Showing < ActiveRecord::Base
belongs_to :film
has_many :bookings
belongs_to :screen
def showing_times
"#{show_date.strftime("%e %b %Y")} # #{show_time.strftime("%H:%M")}"
end
end
But for some reason with the line: <%= select_tag ('title_id'),
options_from_collection_for_select(#films, :id, :title_info, 0 ),
:prompt => "Film" %> I get the error:
NoMethodError in Bookings#new
undefined method `map' for nil:NilClass
The weird part is that I am using a lot of this code else where, I have a _multi_search.html.erb form:
<%= form_tag my_path, :method=>'post', :multipart => true do %>
<!-- Genre: -->
Search By:
<%= select_tag ('cat_id'),
options_from_collection_for_select(#categories, :id, :category_info, 0 ),
:prompt => "Genre" %>
<%= select_tag ('cert_id'),
options_from_collection_for_select(#certificates, :id, :certificate_info, 0 ),
:prompt => "Age Rating" %>
<%= text_field_tag :search_string, nil, placeholder: "ACTOR" %>
or
<%= select_tag ('title_id'),
options_from_collection_for_select(#films, :id, :title_info, 0 ),
:prompt => "Film" %>
<%= submit_tag 'Search' %>
<% end %>
And is used in the application.html.erb:
<%= render(:partial => '/multi_search', :locals=> {:categories => #categories = existing_genres, :certificates => #certificates = Certificate.all, :films => #films = Film.all, :my_path => '/films/multi_find' }) %>
And that works fine.
What am I doing wrong?
It looks like #films is nil. Try setting #films = Film.all (instead of #film = Film.all) in _form.html.erb.
Update:
I would recommend moving the queries to the controller action. In the Model-View-Controller pattern, Controllers should be asking Models for data, not Views.
# BookingLookupController
def new
#films = Film.all
#showings = Showing.all
#seats = Seat.all
end
You can then reference the instance variables in the view.
<%= render partial: '/booking_lookup', locals: {films: #films, showings: #showings, seats: #seats, my_path: '/films/booking_lookup' } %>
In Controller, select fields as you just want to display names in dropdown
def method_name
#films = Film.select([:id, :title_info])
#showings = Showing.select([:id, :showing_times])
#seats = Seat.select([:id, :seats_available])
end
In page
<%= render(:partial => '/booking_lookup', :locals=> {:films => #films, :showings => #showings, :seats => #seats, :my_path => '/films/booking_lookup' }) %>
In partial
options_from_collection_for_select(films, :id, :title_info, 0 ),:prompt => "Film" %>

Extract search functionality to Form Object in Rails

In my Rails application I have simple search functionality. I want to extract to Form Object but don't know how to do. I have search form which looks like this:
.row
= horizontal_simple_form_for :cars, {url: cars_path, method: :get} do |f|
.col-md-4
.row
.col-md-12
= f.input :handover_location, label: I18n.t('.handover'), collection: Location.all.map{|hl| [hl.location_address, hl.id]}
= f.input :return_location, label: I18n.t('.return') ,collection: Location.all.map{|rl| [rl.location_address, rl.id]}
= f.input :car_class, label: I18n.t('.car_class') ,collection: CarClass.all.map { |c| [c.name, c.id] }, include_blank: true
.col-md-4
= f.input :handover_date, as: :string, label: false
= f.input :return_date, as: :string, label: false
= f.submit class: 'btn btn-success'
Cars controller:
class CarsController < ApplicationController
skip_authorization_check
def index
#cars = Car.search(params)
end
def show
end
end
And class method in Car model which search correct cars:
def self.search(params)
self.joins(:reservations).where.not("reservations.reception_time <= ? AND reservations.return_time >= ?",
params[:cars][:return_date], params[:cars][:handover_date]).
joins(:car_class).where("car_classes.id= ?", params[:cars][:car_class])
.cars_at_both_locations(params[:cars][:handover_location], params[:cars][:return_location])
end
Now I'm trying to extract this to Form Object. I've created a file search_form.rb:
class SearchForm
include ActiveModel::Model
attr_accessor :handover_date, :return_date, :handover_location, :return_location, :car_class
end
But now I don't know how to handle my params to this form object. Thank's in advance.
I wish I could help you with the Form Object stuff, but I need to learn more about classes & modules
I can help you with the search functionality, as we've done it before here
Here's the code we used:
#View
<%= form_tag search_path, :method => :post, :id => "SearchForm" do %>
<%= text_field_tag :search, params[:search], placeholder: 'Search your favourite products or brands', :autocomplete => :off %>
<%= image_submit_tag 'nav_bar/search.png' %>
<% end %>
#config/routes.rb
match 'search(/:search)', :to => 'products#search', :as => :search, via: [:get, :post]
#app/controllers/products_controller.rb
def search
#products = Product.search(params[:search])
respond_to do |format|
format.js { render :partial => "elements/livesearch", :locals => {:search => #products, :query => params[:search]} }
format.html { render :index }
end
end
Notice the form_tag we used?
Simple form does not work with form_tag currently (it requires an object) - we just send the data with a GET request to the controller & that then sends the data to the Product model
I think your problem will be caused by the use of your SearchForm object. You only need this because your use of simple form means you have to pass an object. Problem being this is not necessary for search
A better way will be to use a standard form_tag, and send the request directly to your controller. This will allow you to process the data as params, which you'll be able to send directly to your Car model
--
I can write some code specific to you if you want
I found solution on my own.
Cars controller:
def index
#search_form = SearchForm.new(params[:search_form])
#cars = #search_form.submit(params[:search_form])
end
search_form.rb:
class SearchForm
include ActiveModel::Model
attr_accessor :handover_date, :return_date, :handover_location, :return_location, :car_class
def submit(params)
Car.search(params)
end
end
Search form in view:
.row
= horizontal_simple_form_for SearchForm.new, {url: cars_path, method: :get} do |f|
.col-md-4
.row
.col-md-12
= f.input :handover_location, label: I18n.t('.handover'), collection: Location.all.map{|hl| [hl.name, hl.id]}
= f.input :return_location, label: I18n.t('.return') ,collection: Location.all.map{|rl| [rl.name, rl.id]}
= f.input :car_class, label: I18n.t('.car_class') ,collection: CarClass.all.map { |c| [c.name, c.id] }, include_blank: true
.col-md-4
= f.input :handover_date, as: :string, label: false
= f.input :return_date, as: :string, label: false
= f.submit class: 'btn btn-success'
search method in car model:
def self.search(params)
self.joins(:reservations).where.not("reservations.reception_time <= ? AND reservations.return_time >= ?",
params[:return_date], params[:handover_date]).
joins(:car_class).where("car_classes.id= ?", params[:car_class])
.cars_at_both_locations(params[:handover_location], params[:return_location])
end

dropdown list in ruby on rails

I want to display values to drop down list from Database.
For that in my controller class i did the following to get the values from db and its getting properly.
#value = Message.find(:all)
<Message ID: 14448, SlNo: 609">, #<Message ID: 14448, SlNo: 610">
How can i display the SlNo values to drop down list.Here is the code am using and getting error!I don't know how to set values inside a collection_select.Please help me!!
<% #value.each do |d| %>
<%=collection_select(:value, :id, #value, :id, { selected: params.fetch(:value, {})[:id].to_i, :prompt => "-Select a device" }) %>
<% end %>
Table names getting
["UniqueDeviceID", "SlNo"]
Model
class MessageDetail < ActiveRecord::Base
# attr_accessible :title, :body
set_table_name 'DeviceDetails'
set_primary_key 'SlNo'
end
instead of
<% #value.each do |d| %>
<%=collection_select(:value, :id, #value, :id, { selected: params.fetch(:device, {})[:id].to_i, :prompt => "-Select a device" }) %>
<% end %>
use
<%= collection_select :value, :id, #value, :id, :S1No, { selected: params.fetch(:device, {})[:id].to_i, :prompt => "-Select a device" } %>
UPDATE: explanations for passed parameters
:value = a symbol representation of the record you want to update, it may also not be an instance record but just a symbol that will be used in the naming convention of the select tag
:id = the column that you wish to update
#value = the collection to show the choices
:id = the method you want to use that will be passed as the value of the selected value
:S1No = the method that will be used as the label for the options of the select tag
# START
f.collection_select :id, Message.all(:order => "name"), :id, :name, :include_blank => true
# END
OR
# START
messages_arr = []
messages = Message.all(:order => "name")
messages.each do |msg|
messages_arr << [msg.name, msg.id]
end
f.select(:id, options_for_select(messages_arr), {:include_blank => 'Include All'}, {:class=>"span12"})
# END

Resources