21 February 2017

Ruby on Rails | Routes

The Rails router recognizes URLs and dispatches them to a controller's action. It can also generate paths and URLs, avoiding the need to hardcode strings in your views.

-Rails Guide-
If you type in www.example.com in your browser, this request will be sended to your webserver. Your router (routes.rb) recognize the URLs as a root, and dispatches it to whatever Controller's action you've specified in your routes.rb file. root 'pages#home' will dispatch your request to PagesController and call the home action.

If you have a form in your page, you will need to have a route so whenever you pressed the submit button, it will understand what action to perform.

To add routes you can add this code to your routes.rb file: resources :pluralObjectName e.g. resources :photos this will create 7 different routes in your application. You can also create any route manually. for a form for example, you can add post 'users', to:users#create" with this whenever you press submit, it will call the 'create' action in the UsersController and eventually redirect user to the page you've specified in the UsersController

No comments:

Post a Comment