Set option selected with rails symbol - ruby-on-rails

I've this select in my form:
<%= select_tag :x, options_from_collection_for_select(#dmj, :id, :name, :selected), {class: "form-control"} %>
but when I load the page, the option whose value is set in db is not selected.
The symbol :selected comes from a SQL query in the controller and if I try to replace the :name with :selected in the options_from_collection_for_select, I can see that its value is correct.
Also, if I manually set the integer in the options_from_collection_for_select the corresponding option is selected.
Why do I not succeed in selecting the option with the symbol?
EDIT:
My query is making a join to retrieve the selected from a Join table. Here's the query:
#dmj = DiscoveryModeInjury.find_by_sql("SELECT D.name, D.id, L.discovery_mode_injury_id AS selected
FROM
discovery_mode_injuries D
LEFT OUTER JOIN
link_dismodeinj_hospitalizations L
ON
D.id = L.discovery_mode_injury_id
WHERE
flag = 'disc'
ORDER BY
D.name")
If I have understood correctly Akash Srivastava's suggestion, the query should return the DiscoveryModeInjury id field? How..? thanks.

Make sure your query for :selected returns an :id field. Basically, the selected option in options_from_collection_for_select() should be of the same field as the value field of the same, which is :id in your case. Many of us make this mistake of keeping the result of selected as an object of the collection.

Related

Field gets radio button's item id instead of its value

I have snippet:
<%= f.input :purpose, as: :radio_buttons, collection: category.subcategories,
wrapper: :vertical_collection_inline %>
which lines values of category.subcategories horizontally how I want
The problem is, when I select either of option, it assigns that option's ID, but not its value.
How should I refactor the code?
Using IDs has advantages as warned in the comments, however what you’re trying to do should work with either:
category.subcategories.collect(:&values)
Where values is the name of the field which hols “Rent” etc.
The more railsy way to do this is with collection_radio_buttons, like this:
f.collection_radio_buttons(:purpose, category.subcategories, :value, :value)
Again where “value” is the field name.

Populate two database values from one dropdown select

I have a dropdown menu:
<%= f.collection_select :price, Print.all, :printprice, :sizetotal, {prompt: "Pick A Size & Medium"} %>
It currently populates the :price database column with the value :printprice, and shows the value :sizetotal in the actual dropdown selection.
I want to be able to populate two database columns, one of which is the :price column which is already working, but I also want to populate another column called :size with the value :printsize. I want to do this using the same dropdown select menu.
Something like:
<%= f.collection_select :price, :size, Print.all, :printprice, :printsize, :sizetotal, {prompt: "Pick A Size & Medium"} %>
But obviously the above doesnt work
Is this possible?
Following my comment, here is the ID alternative. Basically you want to send only the ID of the print, and find again the whole object on the backend during the #create or #update actions
print_for_size_and_price = Print.find(params[:print_id_for_size_and_price])
So on the frontend I'd use something like this
select_tag(
:print_id_for_size_and_price,
options_from_collection_for_select(Print.all, :id, :sizetotal)
)
And in the backend I'd do somewhere
my_instance.price = print_for_size_and_price.printprice
my_instance.size = print_for_size_and_price.sizetotal

Options select in rails

How I can manually enter the options of a select, I have been using the following:
<%= f.collection_select :establecimiento_id, Establecimiento.order(:nombre), :id, :nombre, include_blank: true %>
But that is used to select data from a table, I want to create one with the months, will it be necessary to create a table with the months or is there a way to enter in the code what I want?
The second param (in your case Establecimiento.order(:nombre)) is the collection for your select. You can pass a array with pairs of values for value and text of that option. For instance, [[1,"January"], [2,"February"] ] and so on will give you the options with the month name, and the value that will be submitted will be the first one of the pair (in that case, the number of month, but could be the name anyway. It depends of what you want to achieve).
Hope it helps. Good luck!
Try:
<%= f.collection_select :establecimiento_id, collection:[[1,"January"], [2,"February"] ], :id, :nombre, include_blank: true %>
I am not sure about the syntax but above answer gave it already away. You can pass options manually. Check API for collection_select:
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select

How to get only the unique values from a select drop down rails

I am trying just to display unique values in the year. This is what I tried so far
= select_tag("year", options_for_select(Car.all.uniq.map{|c|[c.year, c.year]}))
I do know that uniq is for an array. The other thought was to do a validation for uniqueness but the client doesn't want that.
Here you can use select field to get unique dropdown list, check the below code for help:
<%= f.select :year, options_for_select(Car.all.map {|p| [ p.year ]}.uniq, "Select Year"),:prompt => "Select Year",:required => true %>
You are using uniq on Car model. You should map from the collection all the years, then remove duplicates:
= select_tag("year", options_for_select(Car.all.map(&:year).uniq))

rails collection_select wrong number of argument error

I have a user_inputs table where I am storing the device subscription statuses under a column sub_status and these subscription statuses I want as drop down options Under the same name. Now after selecting one option from the drop down I want to save the id of the status in equipment_assets table under a column_name subscription_status and display the status on the browser.
I am trying collection_select for it but its not working.
<div class="pluginESV_formfield">
<%= f.label :subscription_status %><br />
<%= collection_select :sub_status,UserInput.all,:id, :subscription_status %></div>
this gives error, wrong number of arguments, Please help me with this.
here-
:sub_status is the field which has the drop down options.
UserInput is the model from which these status are coming.
:id is the index of the sub_status from the user_inputs table
:subscription_status is the column in the equipment_assets table where selected IDs will be stored. I am not getting what's wrong with the
code.
Please help me out with this.
For table equipment_assets with field subscription_status, you need to update collection_select as below:
<%= collection_select :equipment_asset, :subscription_status, UserInput.all, :id, :sub_status %>
As per the collection_select syntax, i.e.,
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
you missed the object argument which is why you got the error as wrong number of arguments. Its a mandatory argument as it helps to form select HTML element with proper id and name so that upon submission of form the selected value of select drop down will be passed in params hash correctly.

Resources