I am trying to allow a user to input two different things in two different drop down menus from the same form and it will store an integer into a review table.
I want the user to be able to select model_name
in one drop down and manufacturer
in another drop down. The result will store a bat_id
integer into the form. (Telling you which bat the user is selecting)
I have seen a couple questions about date & time but they store the values directly in the model. I am trying to store an integer - bat_id
so that the bat_id will directly link the review model to the bat model.
Examples I have found that are close:
- How do ruby on rails multi parameter attributes really work (datetime_select)
- Rails multiple fields to one model attribute
- Using multiple input fields for one attribute
- Rails Update Single Attribute with Multiple Fields
My form now:
<%= form_for(@review) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field" align= "center">
<h3>Select Brand</h3>
<%= f.collection_select :manufacturer_id, Manufacturer.all, :id, :manufacturer, include_blank: true %>
<h3>Select Bat</h3>
<%= f.grouped_collection_select :bat_id, Manufacturer.all, :bats, :manufacturer, :id, :model_year_and_name, 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 %>
I am submitting to the review
table and trying to submit both of these to the bat_id
attribute.
<h3>Select Brand</h3>
<%= f.collection_select :manufacturer_id, Manufacturer.all, :id, :manufacturer, include_blank: true %>
<h3>Select Bat</h3>
<%= f.grouped_collection_select :bat_id, Manufacturer.all, :bats, :manufacturer, :id, :model_year_and_name, include_blank: true %>
In my bat model I have: has_many :reviews
& In my reviews model I have: belongs_to :bat
UPDATE: Is it possible to use a hidden field with the combination of javascript and my two inputs to determine my one output bat_id?
Update I changed my dropdown code to what works so that I enter in manufacturer_id
& bat_id
when both are selected. However I still think there is a way to store one value in my review
model. I am using javascript very similiar to this
As discussed in your other answer, you shouldn't need to store the
manufacturer_id
on your review model.I would recommend creating a
Manufacturer
select that isn't accessed in your Review model, but is simply used to filter the list of bats on the form.The best way to do this is probably to add some custom data attributes to the
Bat
select.Then use some javascript to filter the
Bat
select when theManufacturer
select is changed.Unfortunately you cannot just set
display: none
to an option element to hide it. This does not hide the option in many browsers. So the best method is to use a bit of jQuery to clone the original select every time the manufacturer select is changed, and remove any option that isn't associated with the selected manufacturer. Like so:You might need to change a few things to get this to work in your installation, but you can view a static demo on jsFiddle here: http://jsfiddle.net/JL6M5/
You will probably need to reject the
manufacturer_id
field on form submit, avitevet already pointed out this answer which should help there: Rails: Ignoring non-existant attributes passed to create()From a UI perspective this seems broken... users will be able to associate any model year & name with any manufacturer, even if that manufacturer did not produce that model year & name.
Assuming you will introduce some javascript to handle that, from a rails perspective you will get undefined behavior with two :bat_id fields in the same form. I think you need this:
Alternatively you can just create one dropdown containing a composite field, like this:
and then in your Bat model introduce something like this: