Rails Route
Routes
We need to manage route. Route means that access place from the user.
The file is config/routes.rb
Check route(route list)
Use URL access
http://localhost:3000/rails/info/routes
By command
rake routes
Get started
This is simple example of routes.rb
SampleApp:Application.routes.draw do get "static_pages/home" get "static_pages/help" end
This is managed by static_pages_controller.rb of controller class
Same
SampleApp:Application.routes.draw do get "static_pages#home" get "static_pages#help" end
class StaticPagesController < ApplicationController def home end def help end end
How to access
In this case,
http://localhost:3000/static_pages/home
http://localhost:3000/static_pages/help
There are points.
app/views/static_pages/home.html.erb, app/views/static_pages/help.html.erb
are applied.
Static routing
get "greeting.html" => "hello#world"
If you access http://localhost:3000/greeting.html, rails run hello/world
Root
You can set your application root as you like.
root 'static_pages#home'
You set static_pages/home.html.erb as root
or
match '/', to: 'static_pages#home', via: 'get'
Match
Match is useful feature of routes.rb
To uses this, you can access xxx_path in your code.
match '/help', to: 'static_pages#help', via: 'get' match '/about', to: 'static_pages#about', via: 'get' match '/contact', to: 'static_pages#contact', via: 'get'
This codes generatte
get ‘static_pages/about’, about_path -> ‘/about’, about_url -> ‘http://localhost:3000/about’
So, you can use about_path, and about_url in your codes(include tests)
URL Parameters
get 'item/:year/:month/:mday' => 'blogs#show'
You access /item/2014/10/21, show of blogs controller handles
You can access parameter params[:year]
Rails.application.routes.draw do get 'item/:year/:month/:mday' => 'blogs#show', constraints:{ year: /20\d\d/, month: /\d\d/, mday: /\d\d/ } end
Add name to route
get 'admin/signin' => 'admin/sessions#new', as :admin_signin
Use name in link method
<%= link_to 'Sign in', :admin_signin %>
Helper method
get 'login' => 'sessions#new', as :login
We have 2 helper methods, login_path, login_url
These are path and url.
You want to add parameter(query string), you can use login_path
<%= link_to 'Sign in', login_path(tracking: '001') %>
Resources
Example) item editor under admin namespace
Action Description | HTTP method | URL | Action Name |
---|---|---|---|
Action | HTTP method | URL path | Action name |
List | GET | /admin/items | index |
Detail | GET | /admin/items/:id | show |
Create form | GET | /admin/items/new | new |
Edit form | GET | /admin/items/:id/edit | edit |
Create action | POST | /admin/items | create |
Update action | PATCH | /admin/items/:id | update |
Delete action | DELETE | /admin/items/:id | destory |
resources :items
Also, added route name
URL | Route Name |
---|---|
/admin/items | :admin_items |
/admin/items/:id | :admin_item |
/admin/items/new | :new_admin_item |
/admin/items/:id/edit | :edit_admin_item |
Link in erb
<%= link_to 'Item', :admin_items %>
Option for resources
Limit routes
index, new, create
resources :items, only: [ :index, :new, :create]
except for show, destroy
resources :items, except: [ :show, :destroy]
Change controller
resources :items, controller : 'secrets'
secrets controller
Change path
resources :items, path: 'i'
items to i
Generate path
_path | _url | URL |
---|---|---|
admin_items_path | admin_items_url | /admin/items |
admin_item_path(id) | admin_item_url(id) | /admin/items/:id |
new_admin_item_path | new_admin_item_url | /admin/items/new |
edit_admin_item_path(id) | edit_admin_item_url(id) | /admin/items/:id/edit |
Resource
Singular resource, get data from session id
The user can edit only its data. Can get data from session id.
We don’t need list in this sentences.
This is singular resource.
Action | HTTP | URL | Action name |
---|---|---|---|
Detail | GET | /user | show |
Register form | GET | /user/new | new |
Edit form | GET | /user/edit | edit |
Create new | POST | /user | create |
Update | PATCH | /user | update |
Delete | DELETE | /user | destroy |
resource :user
Route
URL | Route |
---|---|
user | :user |
user/new | :new_user |
user/edit | :edit_user |
We can also use only and except like resources.
Generate path
_path | _url | path |
---|---|---|
user_path | user_url | /user |
new_user_path | new_user_url | /user/new |
edit_user_path | edit_config_url | /user/edit |
Constraints
parameter constrains
resources :items, constraints: { id:/{0-9}{1,2}/ } # 2 digits
block type
constraints(id:/{0-9}{1,2}/) do resources :items resources :reviews end
concern
Common route setting
resources :reviews do get :unapproval, on: :collection get :draft, on: :member end resources :users do get :unapproval, on: :collection get :draft, on: :member end
Use concern
concern :additional do get :unapproval, on: :collection get :draft, on: :member end resources :reviews, concerns :additional resources :users, concerns :additional