resources :users
Basically, adding the line above to your config/routes.rb will make sure that REST style urls are available to access the User model.
If you have a line like:
get “users/new”
in your router, you can remove it now, since the resource line will take care of adding all the “add”, “create”, “update”, “delete” capabilities to the router
If you only want to make certain resources available (not all):
resources :sessions, only: [:new, :create, :destroy]
An example of one of the simplest forms for routes:
get ‘/teams/home’ => ‘teams#index’
The URL /teams/home will call the controller teams, and the action method index inside of that controller. If you want to pass extra params to this method, you can do stuff like:
get ‘/teams/search/:query’ => ‘teams#search’
To be able to reuse the route later inside rails, to, for example, create links, you can assign a name to the route as follows:
get '/teams/search/:query' => 'teams#search', :as => 'search' And then, linking can be done via: link_to "Search here" search_path if you use search_url, you will get the entire URL (not just the relative path)
rake routes # will list all your available routes
You can also get that via http://localhost:3000/rails/info
Redirection
Redirection is implemented in the browser, but the server send the actual response that triggers it. In rails, this is done via: redirect_to
This method takes a URL as a parameter, but the actual URL can be dynamically created via rails, for example:
redirect_to(article_path(:id=> @article))
redirect_to(@article) # same as above… simplified
redirect_to also takes a :notice param, that acts the same way as having a flash[:notice]
Nested resources
If you see something like this:
resources :articles do resources :comments end
What it is really saying is that comments can’t exists as a standalone page, they depend on an article page to be displayed.