Selected attribute depending on params value with Rails - ruby-on-rails

I have an action that renders a view which contains this:
<select id ='dynamic_select'>
<option value = "<%= activity_path %>">All</option>
<option value = "<%= activity_path(:type => 'enrolled') %>">Enrolled</option>
<option value = "<%= activity_path(:type => 'redeem') %>">Redeem</option>
<option value = "<%= activity_path(:type => 'social') %>">Social</option>
</select>
What would be the correct/Rails way of rendering that select and mark as selected one of the options depending on the type parameter:
If there is no type parameter, select "All" options, if there is type=enrolled parameter select Enrolled option, and so on...
I have managed to do that client side with Javascript, but I am wondering what would be the Rails way of doing so.
Rendered HTML:
<select id="dynamic_select" name="dynamic_select">
<option value="/activity">All</option>
<option value="/activity?type=enrolled">Enrolled</option>
<option value="/activity?type=redeem">Redeem</option>
<option value="/activity?type=social">Social</option>
</select>

Something like the following. I have displayed the select_options here, but you should probably generate them in your controller and pass them through to the view.
The key is using options_for_select.
<% select_options = {"All" => activity_path} %>
<% %w{Enrolled Redeem Social}.each {|opt| select_options[opt] = activity_path(:type => opt.downcase)} %>
<% form_for(resource) do |f| %>
<%= s.select :dynamic, options_for_select(select_option, :selected => select_options[#default || "All"]) %>
<% end %>

Related

Rails Form select : preselect one choice

I have this rails code :
<% status_a = [ ["DRAFT", "DRAFT"], ["OPEN", "OPEN"], ["CLOSE", "CLOSE"] ] %>
<%= form_for(:dash_action, url: brokers_dashboard_path ) do |f| %>
<%= f.select(:select_status, options_for_select(status_a), {}, selected:'OPEN' %>
<% end %>
When it runs, it generates this HTML code :
<select selected="selected" name="dash_action[select_status]" id="dash_action_select_status">
<option value="DRAFT">DRAFT</option>
<option value="OPEN">OPEN</option>
<option value="CLOSE">CLOSE</option>
...
But what I expect is :
selected="OPEN" and not "selected"
Why the select method is not doing what I want ?
Try following code snippet, default value should be the parameter of options_for_select
f.select :select_status, options_for_select(status_a, 'OPEN')

How to dispaly selected value in edit page

How to dispaly selected value in edit page.
in my form i need to display selected value. it is not selecting the value which i selected and submitted. else it shows "please select Study material".
This is my StudyMaterial model
class StudyMaterial < ActiveRecord::Base
TYPES = ['Question Paper', 'Book', 'Audio', 'Video']
enum study_material_type: TYPES
end
This is my 'form.html.erb'
<select class=" required form-control" name="study_material[study_material_type]" id="study_material_study_material_type" data-validation="required" data-validation-error-msg="Select study material">
<option value="">Please select study material</option>
<option value="Question Paper">Question Paper</option>
<option value="Book">Book</option>
<option value="Audio">Audio</option>
<option value="Video">Video</option>
</select>
How to dispaly selected value in edit page.
I am getting this error when i click edit studymaterial page
Please help me to solve this error
<%= form_for #study_material do |f| %>
<%= f.select :study_material_type, StudyMaterial::TYPES.map{|v| [v,v]}, selected: f.object.try(:study_material_type) , required: true, include_blank: "Select" %>
<% end %>
I think, you look something like that:-
<%= form_for #study_material do |f| %>
<%= f.select :study_material_type, StudyMaterial::TYPES, include_blank: "Please select study material", required: true %>
<% end %>
It will display selected value.

RoR - Select tag with include_blank disable

I want a result like this :
<select dir="rtl">
<option selected disabled>Choose a car</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
With the following code I can only end up with :
<%= f.select(:car, xxxxxx, {:include_blank => 'Choose a car', :disabled => 'Choose a car'}) %>
=>
<select id="xxx" name="xxx">
<option value="">Choose a car</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
The first option is not disabled...
Ito A's answer will not work in Rails 4.2 (Unsure about earlier versions).
From the documentation...
:disabled - can be a single value or an array of values that will be disabled options in the final output.
Therefore, the :disabled option should be given a value that matches the value of one of the options in the collection. So, :disabled => 'volvo' would disable the option with value='volvo'. However, it will not match the include_blank option, because that option is not part of the collection passed into the select method.
Rails select helper does not directly support the desired behavior. However, you can work around it by adding a blank option to the collection as follows.
Create the collection and then add a blank option to it.
car_names = %w(volvo saab mercedes audi)
car_names_with_blank = car_names.map{|c| [c, c]}.prepend(['Choose a car', nil])
In the view:
<%= f.select(:name, car_names_with_blank, {disabled: '', selected: ''}) %>
Here's a link to a Github repository with a working example. The example also shows that Ito A's answer and other answers to similar SO questions will not work.
UPDATED: I Have updated my answer with a solution and additional info.
I believe they add the functionality in Rails 6.
From the pull request:
Enable select tag helper to mark prompt option as selected and/or disabled for required field. Example:
select :post,
:category,
["lifestyle", "programming", "spiritual"],
{ selected: "", disabled: "", prompt: "Choose one" },
{ required: true }
Placeholder option would be selected and disabled. The HTML produced:
<select required="required" name="post[category]" id="post_category">
<option disabled="disabled" selected="selected" value="">Choose one</option>
<option value="lifestyle">lifestyle</option>
<option value="programming">programming</option>
<option value="spiritual">spiritual</option>
</select>
I believe what you are looking for is as below:
<%= f.select(:car, xxxxxx, {:include_blank => 'Choose a car', :disabled => 1}) %>
An application of the above noted solution follows.
create the application helper function
def build_selections(prompt: "Select One", selections: {})
selections.reverse!.push([prompt, nil]).reverse!
end
then use it in your view:
<%= f.select :category_id, build_selections(prompt: 'Select Category',
selections: #category.collect{|x| [x.name, x.id]}),
disabled: '' %>

rails multiple params(:filter) request

When i use select with multiple options, form send such get request:
/saveProduct?tags=1&tags=2
so I need to get them with params[:tags], but it takes only last string. How to get all of them?
EDIT:
<%= form_tag("/saveProduct", multipart: true, method: :get) do %>
<select multiple="multiple" class="tagsSelect" name = "tags" >
<option value = 1 >123</option>
<option value = 2 >dfsd</option>
</select>
<% end %>
Your select's name attribute should be tags[] to support multiple values.
<select multiple="multiple" class="tagsSelect" name = "tags[]" >
Now when you will do params[:tags] you will get an array of values in your action.
How to pass an array within a query string

Select onchange not being written

Rails 2.3.5, Ruby 1.86
I haven't been able to figure this out. The 'onchange' in the select below is not being written (no onchange written in the HTML). I haven't seen a reference to the syntax being different except in some older examples the onchange is surrounded in brackets:
<%= f.select :directory_id, options_for_select(#directories, #directory_to_select), :onchange => 'folder_lookup()' %>
results in:
<select id="contact_directory_id" name="contact[directory_id]">
<option value="2">test_1</option>
<option value="4">test_2</option>
<option value="33" selected="selected">test_3</option>
</select>
If I simply change "f.select" to "select_tag" the onchange is written correctly (not that I want to do that though):
<%= select_tag :directory_id, options_for_select(#directories, #directory_to_select), :onchange => 'folder_lookup()' %>
results in:
<select id="contact_directory_id" name="directory_id" onchange="folder_lookup()">
<option value="2">test_1</option>
<option value="4">test_2</option>
<option value="33" selected="selected">test_2</option>
</select>
Am I missing a syntax difference for onchange between a select and select_tag helper?
Thanks!
This is what you want:
<%= f.select :directory_id, options_for_select(#directories, #directory_to_select), {}, :onchange => 'folder_lookup()' %>
With select the method signature looks like this select(object, method, choices, options = {}, html_options = {}). onchange is an html_option, since you don't have any options, you need an empty hash so that your last onchange is taken as an html_option.

Resources