Matthew Berman
Matthew Berman

Reputation: 8631

No route matches for something that should be working

I have:

<%= button_to 'Remove', remove_attendee_event_path(:event_id => @event.id, :user_id => user.id), :method => :post %>

in one partial and it works fine. Then, in another partial in the same view, I tried to copy/paste this code while changing @event.id to event.id and user.id to current_user.id and it doesn't work. It tells me RoutingError in Events#index

No route matches {:action=>"remove_attendee", :controller=>"events", :event_id=>4, :user_id=>108}

WHat am i doing wrong?

Upvotes: 1

Views: 328

Answers (2)

Rob Davis
Rob Davis

Reputation: 15802

I think the problem is that you're passing an :event_id parameter to the path helper instead of :id. A member action requires an :id attribute to specify the member to work with, which in the EventsController will be an Event.

In the partial where this line of code worked, rails was filling in the :id for you based on the current request (which must have been another member action where an Event id was available). The :event_id that you supplied as an argument was ignored (or actually it was probably tacked onto the end of the URL after the question mark and then ignored by the remove_attendee method in the controller).

But now when you try the same line in the index view, it has no :id to fill in the gap, so it's cryptically complaining.

Upvotes: 0

Sam 山
Sam 山

Reputation: 42863

It's not the route it is your object. You are padding nil objects to the method.

Check which object is nil with:

<%= debug @event %>
<%= debug user #most likely this one %>

Local variables need to be sent to views like this:

<%= render :partial => 'somethign' :user => user %>

Upvotes: 1

Related Questions