JP.
JP.

Reputation: 1075

Error No route matches [GET] "/root_path" in rails 3.1.3

I am using Rails tutorial with example book by Michael Hartl as a reference for this question.

Here i am using rails 3.1.3 . What is the best way to use Named Routes

routes.rb

 root :to => "pages#home"
  match '/contact', :to => 'pages#contact' 
  match '/about', :to => 'pages#about'      
  match '/help', :to => 'pages#help' 

When I access these routes from a view using about I find no error but when I am access it by "about_path" I get an error. But in the book they use about_path. Have the concept of named routes changed in rails 3.1 ?

        <li><%= link_to "About", '*about*' %></li>
        <li><%= link_to "Contact", 'contact' %></li>
        <li><%= link_to "Home", 'root_path' %></li>

If i use "about_path" in the above code I get an error 'route not found'

Question 1. What is the best way to use named routes inside the views? (Best way means I only need to change the route path at single place )

Question 2. How can i access root with concept of named routes? (I get an error message when i try to access it using 'root_path').

Upvotes: 0

Views: 2360

Answers (5)

Vietnhi Phuvan
Vietnhi Phuvan

Reputation: 2804

(1) In routes.rb

match '/about' => 'pages#about'

will automatically create a variable about_path that stores your path name - Your path name, by the way, is distinct from your URL. The routing statement literally instructs rails that whenever someone types /about as URL, rails is to take the action about as defined in the controller pages. In addition, that instruction is stored in shorthand in the implicit named route about_path, which rails creates by concatenating the method name about to the string _path without any intervention on your part.

(2) You will be using that variable in ... app/views/layouts/_footer.html.erb

  • <%= link_to "About", about_path %>
  • and ditto in ... app/views/layouts/_header.html.erb

    (3) In ... spec/requests/static_pages_spec.rb, you will use

    describe "About page" do
    before { visit about_path }

    And yes, if you really understand routing, you understand 90% of the design of rails so make sure that you understand the contents of routes.rb inside out.

    Upvotes: 2

    vasilakisfil
    vasilakisfil

    Reputation: 2329

    Remove quotes from path variables, i.e. use this in the .erb file:

        <li><%= link_to "About", about_path %></li>
        <li><%= link_to "Contact", contact_path %></li>
        <li><%= link_to "Home", root_path %></li>
    

    Upvotes: 1

    Christian Roszak
    Christian Roszak

    Reputation: 51

    You have to omit the "'" arround ..._path, I think.

    Upvotes: 5

    JP.
    JP.

    Reputation: 1075

    JP:guard2 jayparteek$ rake routes
       root  /                  {:controller=>"pages", :action=>"home"}
    contact  /contact(.:format) {:controller=>"pages", :action=>"contact"}
      about  /about(.:format)   {:controller=>"pages", :action=>"about"}
       help  /help(.:format)    {:controller=>"pages", :action=>"help"}
    

    Accessing Named routes from views

        <li><%= link_to "About", 'about' %></li>
            <li><%= link_to "Contact", 'contact' %></li>
            <li><%= link_to "News-Home", '/' %></li>
    

    Routes

      match '/contact' => 'pages#contact' 
      match '/about' => 'pages#about'      
      match '/help' => 'pages#help' 
    
      root :to => "pages#home"
    

    Upvotes: -1

    Michael Durrant
    Michael Durrant

    Reputation: 96594

    • make sure the root is first.

    • at the command line type rake routes to see what routes and _path variables you have.

    • remove the :to's but leave ther hashrocket =>'s (EXCEPT for root! - leave the :do there)

    Upvotes: 3

    Related Questions