I have the following simple models:
class Event < ActiveRecord::Base
has_many :participations
has_many :users, :through => :participations
end
class Participation < ActiveRecord::Base
belongs_to :event
belongs_to :user
end
class User < ActiveRecord::Base
has_many :participations
has_many :events, :through => :participations
end
What I would like to do in my view is, dependant on the current users role, delete either an event and its participation record, or just a participation record on its own.
I currently have
<%= link_to 'Delete event', event, :confirm => 'Are you sure?', :method => :delete %>
which deletes both event, and its participation. Do I need another action? or can hijack the destroy action of Event? What would it look like?
Thanks
Well, a hack could be something like this, in a view helper:
And in your view you'd use link_to_delete_event( event ) to delete an event alone and link_to_delete_event( event, participation ) to delete the participation. Your controller could be something like this:
EDIT
To make it less of a hack you should create a nested resource for participations under events:
And then you'll have to create a ParticipationsController, which could look like this:
And the link_to helper would change to this: