1
votes

So, I have the following link-to:

 <%= link_to(outing_add_guests_path, :class => 'modal') do %>
 <div id="notImportant"></div>
 <% end %>

When I click on it, Rails tells me that

No route matches {:controller=>"outings", :action=>"add_guests"}

However, here's my routes file:

resources :outings do
  get "/add_guests" => "outings#add_guests"
  post "/add_guests" => "outings#add_guests"
  delete "/remove_guests" => "outings#remove_guests"
end

and the corresponding action from my Outings Controller:

def add_guests
    @outing_guest = OutingGuest.new(:outing_id => params[:outing_id])
    @outing_guest.user_id = params[:user_id]
    if @outing_guest.save
        flash[:notice] = "Guest added successfully"
        redirect_to({ :action => 'outing', :id => params[:outing_id] })
    else
        flash[:notice] = "Guest could not be added"
        redirect_to({ :action => 'outing', :id => params[:outing_id] })
    end
end

Is there any reason Rails would be unable to detect my controller or its actions?

EDIT: Here's part of the results from rake routes

            outing_add_guests GET    /outings/:outing_id/add_guests(.:format)                outings#add_guests
                              POST   /outings/:outing_id/add_guests(.:format)                outings#add_guests
2

2 Answers

1
votes

I notice your link_to is not consistent with the other routes

outing_add_guests_path
outings_add_guests_path

Did you do rake routes to verify that outing_add_guests_path exists?

EDIT:

Your rake routes shows you need an outing_id so your routes aren't setup right (at least not for the POST). I'd fix them the way @RyanBigg is suggesting.

1
votes

You should be defining these routes using the collection method:

resources :outings do
  collection do
    get :add_guests
    post :add_guests
    delete :remove_guests
  end
end

What this will do is define new routes for the specified actions, as well as automatically defining the routing helpers for those routes. For more information please read the Routing Guide.