rails collection_select wrong number of argument error - ruby-on-rails

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.

Related

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

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

How can I allow NULL values in a drop down list using Ruby on Rails?

Suppose I had a dropdown list in my form that allows users to select their favorite hot drink.
<%= form_for #person do |f| %>
<%= f.select :hot_drink, [['Tea', 'tea'], ['Coffee', 'Coffee']]
...
I want users to able to enter a NULL value if they don't like hot drinks. Something like this:
<%= f.select :hot_drink, [['Nothing Selected', NULL], ['Tea', 'tea'], ['Coffee', 'coffee']]
I know about the :include_blank option, but it's not what I'm looking for because it inserts an empty string into the database, which is not the same as a NULL. There are many NULL values already in my database, and I use this form for inserting and editing the Person's records. I need the dropdown list to recognize NULLs so I can edit the other fields in Person without being forced to change the value of hot_drink.
Anyone know how I could go about this?
Have you tried
<%= f.select :hot_drink, [['Nothing Selected', nil], ['Tea', 'tea'], ['Coffee', 'coffee']] %>
Try to add a default value to nil in your migration to handle this case.

Form_for select field, concatenating data into the display string

I am trying to use a form_for collection_select to display some select field options of account types.
Its occurred to me that it would be easier for the user if they could see the price of the type in each select option
this is my currently not-working code:
<%= a.collection_select :account_type, AccountType.all, :id, (:name+" - "+number_to_currency(:price)) %>
how can i concatenate the values so that (:name+" - "+number_to_currency(:price)) will actually work and not throw an error?
See the documentation:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
You can use the :text_method option to set the displayed text in the select dropdown.
In your AccountType model, define a method like this:
def name_with_price
"#{name} - $#{price}"
end
Then, in your view, you can use:
<%= a.collection_select :account_type, nil, AccountType.all, :id, :name_with_price %>

Resources