Question

I'm new to Rails. I am trying to enable the use of named routes in a rails sample app. When I do the following, and try to access the page in my browser, I get Action Controller: Exception Caught as the title, and a no route matches message.

config/routes.rb

SampleApp::Application.routes.draw do
# root to: 'static_pages#home' #matches '/'
  root to: 'static_pages#home'

  match '/help', to: 'static_pages#help', via: [:get, :post] (*)
  match '/about', to: 'static_pages#about', via: [:get, :post] (*)
  match '/contact', to: 'static_pages#contact', via: [:get, :post] (*)
  [...comments...]
end

I have the necessary actions in my controller. I also have the views, but neither one is the error message. The exact error message is as follows:

No route matches [GET] "/static_pages/home"

I have also tried root 'static_pages#home' as an alternative. Still nothing.

(*) When I remove the via: [:get, :post], the browser throws a RuntimeError saying,

You should not use the match method in your router without specifying an HTTP method. If you want to expose your action to both GET and POST, add via: [:get, :post] option. If you want to expose your action to GET, use get in the router: Instead of: match "controller#action" Do: get "controller#action"

Was it helpful?

Solution

Root is the route when you visit localhost:3000, so when you visit 'localhost:3000' it is matched to static_pages/home in your case.

If you need 'localhost:3000/static_pages/home' also to be forwarded to the corresponding controller you need to define the route additionally like.

get 'static_pages/home'

Otherwise I don't think it is necessary to restart your server after changing your route, unless of course something is wrong, but mostly it does not resolve the issue.

OTHER TIPS

  1. REMEMBER that after changing routing you MUST restart your server!!!

  2. it seems to me that you are trying to open in your browser url 127.0.0.1:3000/static_pages/home when your root route defines 127.0.0.1:3000

PS I hope it will help you

I think this post will clear all you doubts, I had the similar issue earlier.

Actually when you select URL in your browser, by mistake you are trying to access

local:3000/static_pages/home

when you should try to access `

local:3000/home

and as you are following the tutorial, at the right time you will understand why "Match" is not accepting but for the time being you should continue with your get"route" format. One important thing is you should do some research before asking any question and as this question has been asked by several other people's here including me , there is no point of posting a duplicate thread. I hope everything is clear to you. enjoy!!!

here is the similar question. https://stackoverflow.com/a/19047811/2545197

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top