Ponchooo
Ponchooo

Reputation: 83

Setting up static routes in Rails

Need some Rails help (using rails 4.0.8 and ruby 2.0.0)...

I'm trying to refactor my routes, so instead of having...

App::Application.routes.draw.do
   get "static_pages/home"
   get "static_pages/help"
   get "static_pages/about"
   get "static_pages/contact"
end

.. in my routes fI'm setting it to be....

App::Application.routes.draw.do
   root 'static_pages#home'
   match '/help', to: 'static_pages#help', via: 'get'
   match '/about', to: 'static_pages#about', via: 'get'
   match '/contact', to: 'static_pages#contact', via: 'get'
end

...But when I do this, I thought that "match '/about' " is supposed to automatically create named routes for use in controllers and views that looks like...

about_path -> '/about'
about_url -> 'http://localhost:3000/about'

...but it doesn't do so in my controller. So I figured, "Okay, I'll manually put them in", and so I made app/controllers/static_pages_controller.rb...

class StaticPagesController < ApplicationController
   def home
      root_path -> '/'
      root_url -> 'http://localhost:3000/'
   end

   def help
      help_path -> '/help'
      help_url -> 'http://localhost:3000/help'
   end

   def about
      about_path -> '/about'
      about_url -> 'http://localhost:3000/about'
   end

   def contact
      contact_path -> '/contact'
      contact_url -> 'http://localhost:3000/contact'
   end
end

...which doesn't do anything. So now when I try to go to the home page, for example, I get the routing error No route matches [GET] "/static_pages/home".

I'm new to rails development so I'm sure there's something fairly obvious I'm missing here. Can anyone shed some light on what's going on? Much appreciated.

EDIT 1 WITH ANSWER

The answer was given by BroiSatse and Ako...

In the routes config I was mising the as: key, so updating my code to

App::Application.routes.draw.do
  root 'static_pages#home'
  match '/help', to: 'static_pages#help', via: 'get', as: :help
  match '/about', to: 'static_pages#about', via: 'get', as: :about
  match '/contact', to: 'static_pages#contact', via: 'get', as: :contact
end

Did the trick. Thank you!

Upvotes: 2

Views: 1959

Answers (2)

BroiSatse
BroiSatse

Reputation: 44725

All you're missing is as key:

App::Application.routes.draw.do
  root 'static_pages#home'
  match '/help', to: 'static_pages#help', via: 'get', as: :help
  match '/about', to: 'static_pages#about', via: 'get', as: :about
  match '/contact', to: 'static_pages#contact', via: 'get', as: :contact
end

Adding as key will make the routes named and will create required url_helpers.

EDIT:

However, you can do much better:

App::Application.routes.draw.do
  root 'static_pages#home'

  scope controller: :static_pages do
    get :help
    get :about
    get :contact
  end
end

Upvotes: 4

Omid Kamangar
Omid Kamangar

Reputation: 5788

To define your named routes you can do this:

match '/about', to: 'static_pages#about', via: 'get', as: :about

This will give you about_path.

You can find out more about naming routes here.

Upvotes: 0

Related Questions