I've got this problem where I get the dropdown showing but the selected option is not showing up. Here's the code I have to generate the selection dropdown in the .erb:
<%= collection_select("url", "source_type_id", #source_types, :id, :name, {:prompt => "Please select..."}) %>
The #source_types is populated in the controller from a lookup table that is tied to the model. #url_object is the model:
#source_types = SourceType.all
Because of the way the model is tied to the lookup table:
belongs_to :source_type
#url_object.source_type_id returns the numeric value, and #url_object.source_type returns the associated name from the lookup table.
<select id="url_source_type_id" name="url[source_type_id]"><option value="">Please select...</option>
<option value="1">Dictionary/Thesaurus</option>
<option value="2">Encyclopedia</option>
<option value="3">Magazine</option>
<option value="4">Map/Atlas</option>
<option value="5">Newspaper</option>
<option value="6">Reference Tools</option></select>
I read the API for this method and the implication is that if the source_type_id is present the collection_select will automagically pick it up and set the selected value, but this is clearly not happening.
I'm hoping someone will see what obvious mistake I've made here...
Hope this will help
<%= f.select :source_type_id, #source_types.collect{|p| [p.name, p.id] }, params[:source_type_id] %>
Related
I have this with simple form:
= simple_form_for(Note.new, remote: true) do |f|
= f.collection_select :dance_id, current_user.dances.includes(:style).all,
:id, :style_name, {prompt: "Please select dance"}, {class: "form-control"}
Code generated:
<select class="form-control" name="note[dance_id]" id="note_dance_id">
<option value="">Please select dance</option>
<option value="39">Adowa Dance (Ghana)</option>
<option value="38">Adowa Dance (Ghana)</option>
<option value="37">Tango (Argentina)</option>
</select>
which works fine as it is displaying all dances by style_name as i want it. But i want to make a difference between two dances and want to also show level and date right next to each dance in the collection because there are many dances with same name bc of the style which is the same!
please help. any better way to do it with simple form helpers?
As stated in the discussion, you needed to declare a method in your model's rb file like this:
def custom_name
"#{style.name}. #{date}"
end
And change the :style_name parameter to :custom_name
I have this lines in my form
<%= f.fields_for :attached_vehicles do |av| %>
<p>Select make</p>
<%= av.select :make, options_for_select(#makes.collect { |make|[make.make_name, make.id] }), { include_blank: "Select make" }, {} %>
...
<% end %>
which renders this in html
<select name="diy[attached_vehicles_attributes][0][make]" id="diy_attached_vehicles_attributes_0_make">
<option value="">Select make</option>
<option value="1">ACURA</option>
<option value="2">ALFA ROMEO</option>
<option value="3">AM GENERAL</option>
<option value="4">AMERICAN IRONHORSE</option>
<option value="5">AMERICAN LAFRANCE</option>
...
</select>
So now it saves the value of selected option to database, and i need it to save content of selected option.
Also I can't just replace make.id with make.make_name in "options_for_select", because I need value to be id so my other dynamic select box gets right options depending on selected option.
------------------------------------------------------------------------------------------------------------------------------------EDIT
So I did as Dharam suggested
...
def diy_params
params[:diy][:attached_vehicles_attributes].each_with_index do |make_id, index|
params[:diy][:attached_vehicles_attributes][index][:make] = Make.find(make_id).make_name
end
params.require(:diy).permit(:title, :summary, :tip, :warning, attached_vehicles_attributes: [:make, :model, :start_year, :end_year], steps_attributes: [:step_content, add_images_to_steps_attributes: [:image]])
end
...
And I keep getting this error
Couldn't find all Makes with 'id': (0, {"make"=>"12", "model"=>"XPEDITOR", "start_year"=>"2002", "end_year"=>"2010"}) (found 0 results, but was looking for 2)
So i have tried
...Make.find(make_id.first.make).make_name
and multiple other things but didn't get it working, what i'm doing wrong?
------------------------------------------------------------------------------------------------------------------------------------EDIT2
The value of make_id seems to be
(0, {"make"=>"12", "model"=>"XPEDITOR", "start_year"=>"2002", "end_year"=>"2010"})
because it tries to find Make with it as id.
And parameters in console looks like
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Kr4yvJUPCzUagUaW1glt69HEychEz+6QyHpGjKVKQ883rTUl0pZD+9SZSaQujHgM9k7jRt0vP1WTV5fMp8xyJw==", "diy"=>{"attached_vehicles_attributes"=>{"0"=>{"make"=>"12", "model"=>"XPEDITOR", "start_year"=>"2002", "end_year"=>"2010"}}, "title"=>"afaf", "summary"=>"AFSfAS", "tip"=>"FAF", "warning"=>"fdsgfsd", "steps_attributes"=>{"0"=>{"step_content"=>"gsdgsdg"}}}, "commit"=>"Create Diy"}
Before processing the params in your controller, you can do the following:
params[:diy][:attached_vehicles_attributes].each do |index, make_attributes|
params[:diy][:attached_vehicles_attributes][index]['make'] = Make.find(make_attributes['make']).name
end
You can replace Make model with the actual model that is backing the #models.
I have this right now:
<%= f.select :credit, (0..500) %>
This will result in this:
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
...
How would I add another option in that select that would be "All" and which value should be nil?
This will almost do what you want:
<%= f.select :credit, ((0..500).map {|i| [i,i] } << ["No limit",nil]) %>
select can take a number of formats for the list of options. One of them is an array of arrays, as given here. Each element in the outer array is a 2-element array, containing the displayed option text and the form value, in that order.
The map above turns (0..500) into an array like this, where the option displayed is identical to the form value. Then one last option is added.
Note that this will produce a value of "" (an empty string) for the parameter if "Unlimited" is selected - if you put a select field into a form and the form is submitted, the browser will send something for that form parameter, and there is no way to send nil as a form parameter explicitly. If you really wanted to you could use some clever javascript to get the browser to not send the parmeter at all, but that would be more work than simply adding:
param[:credit] == "" and param[:credit] = nil
to your controller action.
If I understand the question correctly, you can use options_for_select and prompt to do this a little more cleanly than what is shown in the selected answer:
<%= f.select :credit, options_for_select(0..500), { prompt: "No Limit" } %>
See the docs here:
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select
following is the code of my select tag
<%= select_tag "assignee#{cnt}", options_from_collection_for_select(#arr,tmp="id","name" , default_selection), html_options = { :onblur => "myblur(this);", :onChange=> "submit_selected(this);", :style=> "visibility:visible;" } %>
the html generated for above line is like:
<select id="assignee1" name="assignee1" onChange="submit_selected(this);" onblur="myblur(this);" style="visibility:visible;">
<option value="12">Name1</option>
<option value="48">Name2</option>
<option value="15">Name3</option>
<option value="35">Name4</option>
</select>
now in the default_selection paramter, i want to use the option value corresponding to option text. i have the option text.
say, i have "Name1" and i want to use its option value i.e 12 in default_selection parameter
any comments?
any comments?
Yes ;) Indeed your question is not related to this portion of code. You just have to search in your object #arr.
My bet :
default_opt = #arr.select {|o| o.name == 'Name1'}
default_selection = default_opt.id if default_opt
Then generate your <select> as you have already done.
How do I use Rails to create a drop-down selection box? Say if I have done the query:
#roles = Role.all
Then how do I display a box with all the #roles.name's ?
EDIT: After implementing the dropdown box. How do I make it respond to selections? Should I make a form?
use the collection_select helper
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M001593
This will allow you to write something like:
collection_select(:user, :role_id, #roles, :id, :role_title, {:prompt => true})
And get
<select name="user[role_id]">
<option value="">Please select</option>
<option value="1" selected="selected">Administrator</option>
<option value="2">User</option>
<option value="3">Editor</option>
</select>
This will create a drop down that displays the role name in the drop down, but uses the role_id as the value passed in the form.
select("person", "role_id", #roles.collect {|r| [ r.name, r.id ] }, { :include_blank => true })
The form helper has a group of methods specifically written to create dropdown select boxes. Usually you'll use the select_tag method to create dropdown boxes, but in your case you can use collection_select, which takes an ActiveRecord model and automatically populates the form from that. In your view:
<%= collection_select #roles %>
Find out more about the Rails form helper here: http://guides.rubyonrails.org/form_helpers.html
Display role name as comboBox showing text (1st pluck argument) and it represents the role id
Controller
#roles = Role.pluck(:name, :id)
View
<%= select("role", "role_id", #roles) %>
params[:role][:role_id] passed to controller from view.