Rails pass id in selection instead of string - ruby-on-rails

I have a select form:
<%= f.select :business_name, options_for_select(Client.uniq.pluck(:business_name)),{:include_blank => false},{:multiple=>true} %>
It picks out the distinct business_name and renders them in a selection box. I need the form to send the relevant business_id when a business_name is selected instead of sending the name string.
How do I achieve this?

options_for_select takes an array of arrays. Ideally if you want the name and value on the html select option to be different you pass in those pairs as [name, value]. By using pluck to grab only the business name you're passing in [name]--no value to put into the option tag.
change your code to use:
...options_for_select(Client.uniq.pluck(:business_name, :id))...>

You can use this:
options_for_select(Client.uniq(:business_name).collect{ |c| [c. business_name, c.id] })
So you would return all unique values on business name and the collect with name-value pair for select

Related

Why select_tag displays only one column?

I have the following select_tag in my view:
<%= select_tag :users, options_from_collection_for_select(#users, 'id','firstname' , 'lastname') %></p></br>
I want to display the firstname and lastname in the select_tag, but it always displays the first parameter after the 'id' in this case the firstname.
What I'm doing wrong?
Use this instead
<%= select(:users, :id, #users.map {|u| [u.firstname + " " + u.lastname,u.id]}) %>
Im confident that options_for_select only allows you to put 1) The value that you want each option to have, 2) The id or label you want that option to have.
What you'd do is, in your User model, set a method to concatenate both the user first and last name into one and then use that as your option_for_select id.
User Model
def userFullName
self.firstname+' '+self.lastname
end
Then use it in your select_tag as this:
<%= select_tag :users, options_from_collection_for_select(#users, 'id','userFullName') %>
From Api Doc for options_from_collection_for_select you can get this:
options_from_collection_for_select(collection, value_method, text_method, selected = nil)
If you only want to display the options, that's is the right way to do it.
You only specify a 4th parameter if you want to preselect one option.
If you want to concatenate the name, you could do it as Oscar Valdez is telling you to.
Check more info for options_from_collection_for_select here: http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_from_collection_for_select

How do I properly use a select box with ruby on rails?

I have a select box for the event types of an event. event belongs to event_type and event_type has many events. Here's what I have for the select box:
<%= f.select :event_type, options_from_collection_for_select(EventType.all, :id, :name, #event.id), :placeholder => 'Select an event type' %>
But, the problem is that when I submit it, it submits the id of the event type and not the name of it. So, how would I make it submit the name rather than the id? If you need to see the any more code than just tell me, thanks
The second parameter to options_from_collection_for_select is the value that will be submitted with the form. You have :id, so change it to :name.
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_from_collection_for_select
(but this seems like a strange thing to do - typically you would store the event type ID.)
You can use the id after the submit to load the event type again in your controller post action like this:
selected_type = EventType.find(params[:event_type]
It is also a good practice to keep database calls to the controller, so please put the EventType.all statement in there and pass it as local or class variable like you did with event.
If you really want to pass the name in your form instead of the id, you can replace the :id part in your call to something more like this options_from_collection_for_select(#event_types, :name, :name, #event.event_type.name). Keep in mind that this value should be unique!
The method works like this:
options_from_collection_for_select(collection, value_method, text_method, selected = nil)
So the first parameter contains all the options, the second defines the value within those option objects which are put into the value field of the HTML option (which is being submitted by the form), the third defines the text which is displayed to the user and the final parameter defines the value of the selected entry (in case you are editing an entry for example). For the last parameter, you need to use the events' event_type id, or in your case, the name because you set the value of your HTML tag to it.
Use pages like ApiDock or the Rails tutorials to get examples for some of these methods.
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_from_collection_for_select
You should pass name in the Value method, if you want to pass the name,
<%= f.select :event_type, options_from_collection_for_select(EventType.all, :name, :name, #event.id), :placeholder => 'Select an event type' %>
Here is the doc for options_from_collection_for_select

Ruby on Rails: combobox is showing id and not the name

I got the code
<%= f.select :wahl1, options_for_select(#berufs) %>
and im getting id´s or smth #<Beruf:0x45ed8e8> instead of the name of #berufs in the combobox
options_for_select expects a simple array or hash of keys and values. Yet you are passing it a collection of models.
What you want is options_from_collection_for_select, for example:
= f.select :wahl1, options_from_collection_for_select(#berufs, 'id', 'name')

How do I obtain a selected value in Ruby on Rails 2.3.8?

My select_tag is as follows.
<%= select_tag "group", options_from_collection_for_select(#groups, "id", "gname") %>
How do I obtain the selected value in my controller?
Use square brackets.
select_tag "group[]", options_for ....
Note the []. Rails will then store this as {"group" => [one option for each form]}.
If it's important to know which select provided which value, you can nest them, so
select_tag "group[bob]", ...
will provide {"group" => {"bob" => selected_option}}.
Basically, [] stores it in an array, and [key] stores it in a hash with that key.
Then in controller, you can use as:
params["group"], which should be an array of the various selects on the page.
Try puts params and check the your console to see values sent to the controller.
That should be params[:group] in your controller.

Rails store key value for a select input

I have in my .html.erb:
<label for="form_marital_status">Marital Status:</label>
<%= select("form", "marital_status", marital_status_options, {}, { }) %>
Where marital_status_options is defined in my helper as:
def marital_status_options
%w[Select Single Married Common-Law/Partner Divorced Widowed]
end
Is there a way I can define marital_status_options to have a key value pairing for use in the select?
%w[Select Single Married Common-Law/Partner Divorced Widowed]
This is going to make the option value and text for each option the same. Return an array of arrays if you want the option value and text to be different for each option. The first value in each array is the text value for the option; the second is the option value itself.
def marital_status_options
[["Select", ""], ["Single", "single"], ["Married", "married"], ["Common-Law/Partner", "partners"], ["Divorced", "divorced"], ["Widowed", "widowed"]]
end
This is explained clearly in the documentation.
You should also consider not passing the blank "Select" option from your method as there is a way to do this through the select method itself.
# Helper
def marital_status_options
[["Single", "single"], ["Married", "married"], ["Common-Law/Partner", "partners"], ["Divorced", "divorced"], ["Widowed", "widowed"]]
end
# form
<%= select("form", "marital_status", marital_status_options, {:include_blank => "Select"}) %>

Resources