0
0
Ruby on Railsframework~5 mins

Route definition in routes.rb in Ruby on Rails - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the routes.rb file in a Rails application?
The routes.rb file defines how URLs map to controller actions. It tells Rails what code to run when a user visits a specific web address.
Click to reveal answer
beginner
How do you define a simple route that maps the URL /home to the index action of the home controller?
You write: <br>get '/home', to: 'home#index'<br>This means when someone visits /home, Rails runs the index method in the home controller.
Click to reveal answer
intermediate
What does the resources :posts line do in routes.rb?
It creates all the standard routes for a resource named posts. This includes routes for showing, creating, editing, updating, and deleting posts automatically.
Click to reveal answer
beginner
Explain the difference between get and post in route definitions.
get defines a route for retrieving data (like showing a page). post defines a route for sending data to the server (like submitting a form).
Click to reveal answer
beginner
How can you set the root URL (homepage) of your Rails app in routes.rb?
Use the root method like this:<br>root 'welcome#index'<br>This means the homepage URL / runs the index action of the welcome controller.
Click to reveal answer
What does this route do? get '/about', to: 'pages#about'
AMaps the URL /about to the about action in pages controller
BCreates a new page called about
CDeletes the about page
DRedirects /about to the home page
Which HTTP method is used to submit form data in Rails routes?
Apost
Bget
Cput
Ddelete
What does resources :users generate?
AOnly the show route for users
BA single route for creating users
CAll standard RESTful routes for users
DNo routes, just a placeholder
How do you set the homepage route in Rails?
Astart 'welcome#index'
Bhome 'welcome#index'
Cget '/', to: 'welcome#index'
Droot 'welcome#index'
Which file do you edit to define routes in a Rails app?
Aapplication.rb
Broutes.rb
Cconfig.yml
Ddatabase.rb
Describe how you would create a route for a 'contact' page that shows a form using the 'pages' controller and 'contact' action.
Think about how to connect a URL to a controller action using get.
You got /3 concepts.
    Explain what the resources method does in Rails routing and why it is useful.
    It saves time by generating many routes for common actions.
    You got /3 concepts.