views:

582

answers:

2

Hey guys I've been using the has_and_belongs_to_many relationship with checkboxes example from the Railscast Episode #17 . I had some problems and now everything is working kind of smoothly except the update button will not work.

the edit view looks like so

<% form_for :users, :action => 'update'  do |f| %>
<% for interest in Interest.find(:all) %>
<div>
<%= check_box_tag "user[interest_ids][]", interest.id,
              @user.interests.include?(interest) %>
<%= interest.name %>
</div>
<% end %>
<p>
<%= f.submit 'Update' %>
</p>
<% end %>

and in the controller I have ....

def edit
  @user = User.find(session[:user_id])
end

 def update
  params[:user][:interest_ids] ||= []
  @user = User.find(session[:user_id])
  if @user.update_attributes(params[:user])
  flash[:notice]='User data was updated'
  redirect_to :action => 'index'
 else
  redirect_to :action => 'index'
 end  
end

The button isn't event doing the redirect... so I dont know whats happening. Is there something in my form creation messing this up? I'm not exactly sure how to create an button and have it access the method in the controller with model updates and etc.

I looked around for help and thought maybe it was because of attr_accessible so I added =>

 attr_accessible :login, :email, :password, :password_confirmation, :interest_ids, :user

to my user model but still nothing...Any I idea why my form isn't submitting?

A: 

The check_box tag must look like this for it to work:

<%= check_box "user", "user_interest_ids", {:checked => @user.interests.include?(interest), :name => 'user[interest_ids][]'}, interest.id, nil %>

In my ad <-> ad_sizes habtm relationship I'm using this code:

<% @ad_sizes.each do |s| %>
<li>
<%= check_box "ad", "ad_size_ids", {:checked => @ad.ad_sizes.include?(s), :id => "ad_size_#{s.name}", :class => 'checkbox', :name => 'ad[ad_size_ids][]'}, s.id, nil %>
<label class="checkbox" for="<%= "ad_size_#{s.name}" %>"><%= s.description %></label>
</li>
<% end %>
Paweł Gościcki
But isnt that code in the railscast??? I tried your code and it didnt work. The nothing is still being updated, I have a feeling its related to something in the form because its not doing any redirect after the button click.
ChrisWesAllen
The code is similar, true. Btw, there's an error in your `form_for` definition. It should read `<% form_for :user, :action => 'update' do |f| %>`
Paweł Gościcki
Right I meant why isnt that code within the railscast? I adjusted the form_for but the snippet you offered doesnt seem to be working. I wondering why its still not doing any redirect.
ChrisWesAllen
You need to be more specific. What error do you have? If there are no errors then what is the `params` variable value after hitting update?
Paweł Gościcki
+1  A: 

I played around with the form and the correct code is this ==>

<% form_for @user do |f| %>
<% for interest in Interest.find(:all) %>
<div>
 <%= check_box_tag "user[interest_ids][]", interest.id, @user.interests.include?(interest) %>
 <%= interest.name %>
</div>
<% end %>
<p>
<%= f.submit 'Edit' %>
</p>
<% end %>

I'm so happy! So apparently the problem was :user which should the be object @user.

ChrisWesAllen