get '/post/:id', to: 'posts#show'
resorces Generate a resourceful set of routes. (7 actions)
resources :posts
--param option
Segment: id can be changed to any value with the param option.
resources :posts, param: :no
#=> /books/:no
--only, except option
You can limit routing to specific actions, or exclude specific actions from routing.
resources :posts, only: [:show]
resources :posts, except: [:show]
--collection, member option
collection is used for routes that do not require an id, and member uses routes that do require an id.
resources :posts, only:[] do
collection do
get :post_lndex , to: 'posts#post_index'
end
member do
get :post_edit , to: 'posts#post_edit'
end
end
resource
Used when using a singular resource (a resource that does not require an id reference). (Other than index action)
resource :user
root Generate application route routing.
root 'welcome#index'
HttpHelpers Helper methods of typical HTTP methods are defined.
match can use the via option to generate routes for multiple HTTP methods.
match '/posts/:id', to: 'posts#update', as: 'post', via: [:put, :patch]
#=> PUT|PATCH /posts/:id(.:format) posts#update
If the anchor option is set to false, it will be valid for all requests that match the resource starting with path.
match 'posts', to: 'posts#index', anchor: false, via: :get
scope You can create various scopes and describe routing settings.
scope '/posts' do
get ':id', to: 'posts#show'
end
#=> GET /books/:id(.:format) posts#show
Use the controller specified by the controller option in the scope.
scope '/posts', controller: :posts do
get '/', to: :index
post '/', to: :create
end
#=> GET /posts(.:format) posts#index
#=> POST /posts(.:format) posts#create
Use the action specified by the action option within the scope.
scope '/posts/:id', controller: :posts, action: :update do
match '/', via: [:put, :patch]
end
#=> PUT|PATCH /posts/:id(.:format) posts#update
Use the module specified by the module option in the scope.
scope '/admin', module: :admin, as: :admin do
get '/', to: 'admin#index'
end
#=> GET /admin(.:format) admin/admin#index
The name of namespace is added to the beginning of the routing.
namespace :admin, as: 'admin' do
# do something
end
Recommended Posts