0

I am trying to create a form that updates a table. I want to have a dropdown button that pulls information from one table called "manufacturer" and once the form is submitted, stores the data in the "reviews" table.

This is my form:

<%= form_for(@review) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field" align= "center">
    <h3>Select bat</h3>
    <%= f.collection_select :bat_id, Manufacturer, include_blank: true %>
    <h3>What do you like about this bat?</h3>
    <%= f.text_area :pros, placeholder: "Enter what you like..." %>
    <h3>What do you not like about this bat?</h3>
    <%= f.text_area :cons, placeholder: "Enter what you don't like..." %></br>
  </div>
  <div align="center">
  <%= f.submit "Add Review", class: "btn btn-large btn-info" %>
  </div>
<% end %>

When you look at this line- <%= f.collection_select :bat_id, Manufacturer, include_blank: true %> I believe :bat_id tells the the form where to send the input from the user. (in this case to the :bat_id parameter in the review table)

How would I tell the form to select the dropdown options from the manufacturer table?

Update: Would I need to add anything in the controller?

1
  • you can try the simple form gem which provides an association selection input
    – nickcen
    Feb 7, 2014 at 3:43

1 Answer 1

0

Look at the documentation http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-collection_select

collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) 
# method: the field in your form's object
# collection: the collection of active records to display as options
# value_method: which method call in items of the collection to get the displayed value
# text_method: which method call in items of the collection to get the id value

So in your case:

<%= f.collection_select :bat_id, Manufacturer.all, :id, :name, include_blank: true %>

Given that Manufacturer model has a name field, otherwise you need to adapt

3
  • I tried the above but It reads "NoMethodError" and "undefined method `map' for #<Class:0xb66e79c>"
    – Daniel
    Feb 7, 2014 at 5:24
  • Manufacturer.all, not just Manufacturer. Feb 7, 2014 at 5:29
  • thanks I got it to work and I changed your answer by switching :name with :id to show the value of :name in the dropdown and not the id number in the dropdown.
    – Daniel
    Feb 7, 2014 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.