Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No route matches in Rails 3.0.4

Been staring at this problem for a while now. Here's the error I'm getting when I try to view the page.

No route matches {:action=>"confirm", :controller=>"locations"}

This is what I have in the view.

<%= form_for(@location, :url => { :action => :confirm }) do |f| %>
<% end %>

And I think my routes file is set up correctly.

Finder::Application.routes.draw do
  resources :locations do
    member do 
      post :confirm
    end
  end

  root :to => 'locations/index'
end

Any ideas?

Updated:

Ran rake routes and get what I think is correct.

confirm_location POST   /locations/:id/confirm(.:format) {:action=>"confirm", :controller=>"locations"}
like image 264
Garrett Murphey Avatar asked Feb 20 '11 19:02

Garrett Murphey


4 Answers

You can debug your routes easily in the future by running $ rake routes and looking at the output. ;)

I think what is happening is that your post :confirm isn't registering the route you're expecting. In the guides, match and it's brethren accept a string as a URL segment like so:

resources :locations do
  member do
    post 'confirm'
  end
end

Note that "confirm" is now a string instead of a symbol.

If this doesn't help, run $ rake routes and tack the output onto your question.

Update

After seeing your rake output, I think that you just need to specify the POST method on your form_for:

<%= form_for(@location, :url => { :action => :confirm }, :method => :post) do |f| %>
<% end %>

You can also make this more readable using that helper method that Rails defines:

<%= form_for(@location, :url => confirm_location_path(@location), :method => :post) do |f| %>
<% end %>
like image 191
coreyward Avatar answered Oct 14 '22 03:10

coreyward


Did you define the confirm action in your LocationsController?

like image 33
Adrian Pacala Avatar answered Oct 14 '22 02:10

Adrian Pacala


Try adding a :method => :post to your form_for

<%= form_for(@location, :url => { :action => :confirm }, :method => :post) do |f| %>
<% end %>
like image 36
Dogbert Avatar answered Oct 14 '22 03:10

Dogbert


Make sure that form_for doesn't sneak in a hidden field with _method=put if you have declared the route as accepting only post in your routes file.

like image 29
Jonas Avatar answered Oct 14 '22 02:10

Jonas