Jump to content

The ultimate community for Ruby on Rails developers.


Photo

No route matches

No route matches

  • Please log in to reply
5 replies to this topic

#1 rmonica

rmonica

    Passenger

  • Members
  • 3 posts
  • LocationLisbon, Portugal

Posted 23 August 2013 - 11:47 AM

I'm new on RoR and I need help because I can't find the mistake.

 

I have 2 controllers, Clinica (default) and Consultas.

On /views/clinica I have the index.html.erb where I have the command:

 

<%= button_to "Read More", new_consulta_path, :class=>"icon icon-arrow-right button", %>

 

On the /views/consultas I have a new.html.erb file where I have a form. If I type the URL on the browser

 

http://localhost:3000/consultas/new

 

I get the form without problems.

 

 

On my routes.rb file I have:

 

Clinica::Application.routes.draw do

  resources :clinica

  resources :consultas

 

The problem is that when I'm in the index.html.erb (Clinica) and I click on the button "Read More" to go to the new.html.erb (consultas) file I get the error:

 

No route matches [POST] "/consultas/new"

 

Does anyone know why is this happening? Any help is welcome.

Thanks.



#2 Ohm

Ohm

    Guard

  • Members
  • 179 posts
  • LocationCopenhagen

Posted 23 August 2013 - 12:32 PM

This might be because of Rails' inflection rules. Consultas is plural, so a new one would be at the http://localhost:3000/consulta/new path. If this is not correct plural, you can add it to the inflections.rb initializer.

 

You can alway view the routes in your app by running rake routes. I usually run it with a grep, like so: rake routes | grep consulta, that way I only get the lines concerning consulta.


Blog: http://ohm.sh | Twitter: madsohm


#3 rmonica

rmonica

    Passenger

  • Members
  • 3 posts
  • LocationLisbon, Portugal

Posted 23 August 2013 - 02:05 PM

Running the rake routes I get to the following,
 

consultas          GET    /consultas(.:format)             {:action=>"index", :controller=>"consultas"}
                         POST   /consultas(.:format)            {:action=>"create", :controller=>"consultas"}
new_consulta   GET    /consultas/new(.:format)     {:action=>"new", :controller=>"consultas"}
edit_consulta    GET    /consultas/:id/edit(.:format) {:action=>"edit", :controller=>"consultas"}
     consulta       GET    /consultas/:id(.:format)        {:action=>"show", :controller=>"consultas"}
                         PUT    /consultas/:id(.:format)         {:action=>"update", :controller=>"consultas"}
                         DELETE /consultas/:id(.:format)      {:action=>"destroy", :controller=>"consultas"}
new_consulta_path is in the singular.
How can I fix it?
I had already uncomment the inflections file, but I still have that error.
Is there anything missing on the routes.rb file?
 
Thanks!


#4 Ohm

Ohm

    Guard

  • Members
  • 179 posts
  • LocationCopenhagen

Posted 23 August 2013 - 02:08 PM

You shouldn't uncomment the inflections file, but rather add consultas as an exception. E.g. if consultas is both singular and plural, use 

inflect.uncountable %w( consultas )

Read more about it here: http://api.rubyonrai...nflections.html


Blog: http://ohm.sh | Twitter: madsohm


#5 rmonica

rmonica

    Passenger

  • Members
  • 3 posts
  • LocationLisbon, Portugal

Posted 23 August 2013 - 06:55 PM   Best Answer

The problem was that I was using the button_to which uses by default the POST method, so I should use

 

:method => :get

 

somehting like:

 

<%= button_to "Read More", new_consultas_path, :method => :get%>

 

The problem is solved.

Thanks for help.



#6 Rowel

Rowel

    Controller

  • Members
  • 109 posts

Posted 27 August 2013 - 06:06 PM

<%= button_to "Read More", new_consulta_path, :class=>"icon icon-arrow-right button", %>

 

 

I think a link_to is more applicable in this case (Read More).

 

link_to defaults to a GET action.  -- you're not trying to update or delete anything.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users