イベントモデルとユーザーモデルが参加者モデルを介して結合されています。認証されたユーザーとしてイベントに「参加」する方法を理解しました。しかし、私が理解できないのは、イベントから「撤回」するための良い方法です。これは私が見逃している些細なことだと確信していますが、些細なことを尋ねるよりも、StackOverflowへの入り口を作るためのより良い方法はありますか?ああ、私は何時間もrailscastsとSOを検索してきました...
ありがとう!
views / events / show.html.erb
<p><strong>Attendees: </strong>
<ul>
<% for attendee in @event.users %>
<% if attendee.username == current_user.username %>
<li><strong><%= attendee.username %></strong>
<%= link_to 'Withdraw From Event', withdraw_event_path(@event.id), :method => :post, :class => 'btn btn-danger' %>
<%= link_to 'Destroy', @attendee, confirm: 'Are you sure?', method: :delete, :class => 'btn btn-danger' %>
</li>
<% else %>
<li><%= attendee.username %></li>
<% end %>
<% end %>
</ul>
</p>
/controllers/events_controller.rb
def attend
@event = Event.find(params[:id])
current_user.events << @event
redirect_to @event, notice: 'You have promised to attend this event.'
end
def withdraw
# I can't get this to work
redirect_to @event, notice: 'You are no longer attending this event.'
end
models / event.rb
class Event < ActiveRecord::Base
attr_accessible :name, :location
belongs_to :users
has_many :attendees, :dependent => :destroy
has_many :users, :through => :attendees
models / user.rb
class User < ActiveRecord::Base
has_many :events
has_many :attendees, :dependent => :destroy
has_many :events, :through => :attendees
models / attendee.rb
class Attendee < ActiveRecord::Base
belongs_to :event
belongs_to :user
attr_accessible :user_id, :event_id
# Make sure that one user cannot join the same event more than once at a time.
validates :event_id, :uniqueness => { :scope => :user_id }
end