Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Sebastien Ferre
3,319 PointsAssign params via form_for check_box in rails for HABTM relationship.
Hi everyone,
After learning a lot from Treehouse videos I'm trying to build my own project using rails. I can't use simple_form because I'am using bootstrap 3 which is not yet supported by simple_form so I've to deal with form_for.
I got two models linked using a has_and_belongs_to_many relationship : Wine and Shop . To make it simple a wine can be sold in different shops and a specific shop can sell many different wines. Here are the models :
class Shop < ActiveRecord::Base
has_and_belongs_to_many :wines
end
class Wine < ActiveRecord::Base
has_and_belongs_to_many :shops
end
My goal is to make a form to create instances of Wine including the Shops where the wine can be purchased. Here is my wines_controller :
def new
@wine = wine.new
@shops = Shop.all
respond_to do |format|
format.html # new.html.erb
format.json { render json: @vin }
end
end
def create
@wine = Wine.new(params[:wine])
params[:shops].each do |id|
@wine.shops << Shop.find(id)
end
end
Here is my _form view rendered in new view :
<% @shops.each do |t| %>
<%= f.label t.name %>
<%= f.check_box :shops, t.id %>
<% end %>
I've tried many things and spent hours on this but can't found the solution. Lastly I got an
undefined method `merge' for 3:Fixnum
Just let me know if you need any other details to deal with this issue.
Thanks in advance
Sebastien Ferre
3,319 PointsSebastien Ferre
3,319 PointsI figured out this soluton with some help from stackoverflow :
view
model
But I'm still interested in other ways to do the same thing! ;)