Frage

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?

War es hilfreich?

Lösung

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top