views:

66

answers:

1

Hi guys,

I'm having the following many to many relationship in Rails (ActiveResource, of course):

class User < ...
  has_many :channel_assignments
  has_many :channels, :through => :channel_assignments
end


class Channel < ...
  has_many :channel_assignments
  has_many :users :through => :channel_assignments
end

class ChannelAssignment < ...
  belongs_to :user
  belongs_to :channel
end

Defined routes:

map.resources :users, :has_many => :channel_assignments

Update: rake routes gives the following output:

       user_channel_assignments GET    /users/:user_id/channel_assignments(.:format)                                 {:action=>"index", :controller=>"channel_assignments"}
                                POST   /users/:user_id/channel_assignments(.:format)                                 {:action=>"create", :controller=>"channel_assignments"}
    new_user_channel_assignment GET    /users/:user_id/channel_assignments/new(.:format)                             {:action=>"new", :controller=>"channel_assignments"}
   edit_user_channel_assignment GET    /users/:user_id/channel_assignments/:id/edit(.:format)                        {:action=>"edit", :controller=>"channel_assignments"}
        user_channel_assignment GET    /users/:user_id/channel_assignments/:id(.:format)                             {:action=>"show", :controller=>"channel_assignments"}
                                PUT    /users/:user_id/channel_assignments/:id(.:format)                             {:action=>"update", :controller=>"channel_assignments"}
                                DELETE /users/:user_id/channel_assignments/:id(.:format)                             {:action=>"destroy", :controller=>"channel_assignments"}

As ChannelAssignemnts are bound to user, I'm using my scaffolded ChannelAssignmentsController to automatically assign a user to a channel when creating a ChannelAssignment.

I'm doing that by using these URLs:

#/app/views/users/index.html.erb
#show a link to view all channels of a user
<%= link_to 'Channels', user_channel_assignments_path(user) %>
...

#/app/views/channel_assignments/new.html.erb
#assign a channel to currently selected user
<% form_for(@channel_assignment, :url => user_channel_assignments_path(@user) ) do |f| %>
...

That charmingly works.

But: Which is the path to unassign a channel, ergo: to delete a user's ChannelAssignment? Cannot find it when running rake routes.

Must be something like

<%= link_to 'Destroy', delete_user_channel_assignment, :user_id => @user, :method => :delete %>

Any input on this? I'm sure there is a way to autmatically have this URL generated.

Thanks

Matt

A: 

You just need to use the delete verb in your link:

<%= link_to 'Destroy', user_channel_assignment(:user_id => @user, :id => @channel), :method => :delete %>

It should also be visible in rake routes - just with the delete verb.

Mr. Matt
Hi Matt, thanks for your answer, but that didn't do the trick. I updated my post with the output of rake routes. Your code generates a nice Javascript form but ignores the user_id. Had that before as well. Any other suggestion? Matt
Matt
It might just be a bracketing problem - I've updated the code. The route is there, so as long as it's implemented in the controller, then it should work.
Mr. Matt