1

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.

mrgreen
  • 15
  • 4
  • try `f.select :sub_status_id, options_for_select(UserInput.all.map{|ui| [ui.subscription_status, ui.id]})` – MrYoshiji Apr 14 '14 at 13:39

1 Answers1

1

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.

Kirti Thorat
  • 52,578
  • 9
  • 101
  • 108
  • Hi, thanks for the reply, but its still giving error, with f.collection_select it gives -:Error (undefined method `merge' for :subscription_status:Symbol) and without f. it gives Error (undefined method `subscription_status' for #) – mrgreen Apr 14 '14 at 13:36
  • With `f.collection_select` you are not supposed to use `object` BUT with `collection_select` you must provide `object`. In your question you mentioned `collection_select`. – Kirti Thorat Apr 14 '14 at 13:39
  • As per the error message, your field name is `sub_status` and not `subscription_status `. Hence, the symbol error. See my updated answer. – Kirti Thorat Apr 14 '14 at 13:42
  • yeah that worked, so now I can see the drop down options, but the table where I want to save the selected status has this field subscription_status. so on submit the selected input is going no-where. – mrgreen Apr 14 '14 at 13:48
  • What it is the table name and the field name in which you want to store the selected value from drop down? Update your question with relevant details. – Kirti Thorat Apr 14 '14 at 13:51
  • yeah that worked perfectly. And now My concept about collecion_select is also clear. thank you so much for the help. – mrgreen Apr 14 '14 at 14:01