Ruby On Rails Classroom image

Pooja  Negi / Student / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  |1 | 2|Last
Lessons:-Rails Routes

Rails Routes

 

Now that we have tests for the URIs we want, it’s time to get them to work. As noted in Section 3.1.2, the file Rails uses for URI mappings is config/routes.rb. If you take a look at the default routes file, you’ll see that it’s quite a mess, but it’s a useful mess—full of commented-out example route mappings. I suggest reading through it at some point, and I also suggest taking a look at the Rails Guides article ‘‘Rails Routing from the outside in’’ for a much more in-depth treatment of routes.

                  To define the named routes, we need to replace rules such as

get   'static pages/help'

 

with

match   '/help',    to:    'static pages#help'

 

         This arranges both for a valid page at /help and a named route called help_path that returns the path to that page. (Actually, using get in place of match gives the same named routes, but using match is more conventional.)

  Applying this pattern to the other static pages gives Listing 5.21. The only exception is the Home page, which we’ll take care of in Listing 5.23.

 

Listing 5.21 Routes for static pages.
config/routes.rb

 

                                  

 

If you read the code in Listing 5.21 carefully, you can probably figure out what it does; for example, you can see that

 

match    '/about',     to:            'static pages#about'

 

matches ’/about’ and routes it to the about action in the StaticPages controller. Before, this was more explicit: We used  

 

get     'static pages/about'

 

to get to the same place, but /about is more succinct. In addition, as mentioned above, the code match ’/about’ also automatically creates named routes for use in the controllers and views:  

 

about path   =>     '/about'
about url     =>        'http://localhost:3000/about'

 

Note that about_url is the full URI http://  localhost:3000/about (with localhost: 3000 being replaced with the domain name, such as example.com, for a fully deployed site). As discussed in Section 5.3, to get just /about, you use about_path. (The Rails Tutorial uses the path form for consistency, but the difference rarely matters in practice.)  

                 With these routes now defined, the tests for the Help, About, and Contact pages should pass:

 

$  bundle  exec  rspec spec/requests/static pages spec.rb

 

          This leaves the test for the Home page as the last one to fail. To establish the route mapping for the Home page, we could use code like this:  

 

match   '/' ,   to:       'static pages#home'

 

This is unnecessary, though; Rails has special instructions for the root URI / (‘‘slash’’) located lower down in the file (Listing 5.22).

 
 
 
image
Pooja   Negi

Skills    Ruby On Rails

Qualifications :- High School - SSN high school, College/University - HNBGU, College/University - SRHU,
Location :-Ranipokhari,Rishikesh,Uttarakhand,India
Description:- Student
Explore
 

  Students (0)