What I'm trying to do seems easy, but I've been trying to get it right for 3 days now, and am at my wits end.
What I have: 2 tables + 1 join table. Subscribers, Cities, and Cities_Subscribers
Form looks like this: TEXTBOX - EMAIL CHECKBOXES - CITIES SUBMIT
My models are like so:
class Subscriber < ActiveRecord::Base attr_accessor :email, :subscriber_id, :city_id has_and_belongs_to_many :cities accepts_nested_attributes_for :cities end
class City < ActiveRecord::Base has_and_belongs_to_many :subcribers end
my controller: def create @subscriber = Subscriber.new(params[:subscriber]) if @subscriber.save flash[:notice] = 'Album was successfully created.' redirect_to(:action => 'index') else flash[:notice] = 'Errors@!!.' redirect_to(:action => 'test123') end end
My Form:
<%= semantic_form_for Subscriber.new do |f| %> <%= f.inputs :email %>
<%= f.semantic_fields_for Subscriber.new do |g| %> <% @cities.each do |ct| %> <label><%= ct.name %> <%= check_box_tag "subscriber[city_ids]", ct.id %> </label>
<% end %> <% end %> <%= f.buttons %> <% end %>