I've a simple problem but have been struggling with it for many hours. I've these two models in my rails app:
class User < ActiveRecord::Base
has_and_belongs_to_many :groups
end
and
class Group < ActiveRecord::Base
has_and_belongs_to_many :users
end
and this controller:
class GroupsController < ApplicationController
def show
@group = Group.find(params[:id])
@users = @group.users
end
and I need to display a view which shows one particular group with its relative users and next to each user is a link for removing the user from the association upon hitting the remove button. My current version of views/groups/show looks like this:
<p>Description: <%= @group.description if @group.description %></p>
<p>Associated Users:
<ul>
<% @users.each do |user| %>
<li>
<%= link_to user.email %>
<%= link_to user.name %>
<button>
<%= link_to("Remove user from group", @group.users.delete(user),
:data => { :confirm => "Are you sure?" }) unless user == current_user %>
</button>
</li>
<% end%>
</ul>
</p>
Which "works" but when I then refresh the page delete method is executed and the association is deleted without hitting the button. I tried using the partial and many other different ways but couldn't figure it out. Can you please help?