I have attend and remove_attendee methods I am using shown below:
def attend
@event = Event.find(params[:id])
if @event.users.include?(current_user)
flash[:error] = "You're already attending this event."
else
current_user.events << @event
flash[:success] = "Attending event!"
end
redirect_to @event
end
def remove_attendee
@event = Event.find(params[:event_id])
@event.users.destroy(params[:user_id])
flash[:success] = "User removed from event."
redirect_to @event
end
Then I also have the view in which I list all users attending the event like so:
The attendees of this event are:</br></br>
<% @attendees.each do |user| %>
<li><%= link_to user.name, user %>
<% if @event.users.include?(user) && (user == current_user) %>
<%= button_to 'Remove', remove_attendee_event_path(:event_id => @event.id, :user_id => user.id), :method => :post %>
<% end %>
</li>
<% end %>
Lastly, I have the attend event button:
`<%= button_to 'Attend Event', attend_event_path(@event.id), :method => :post %>`
The problem is when I click attend, then remove attendee, it works great. Then, when I try to attend the event again…it gives me an error “NoMethodError in EventsController#attend”
Then it deletes the current user I am using out of the database when I just want it to delete the relationship between that user and the event…What am I doing wrong?
I think conventionally, this would look something like this: