Routing Flashcards
How to add ‘/hello’ get action?(7 ways)
get ‘application/hello’
get ‘hello’, to: ‘application#hello’
get ‘hello’ => ‘application#hello’
get ‘hello’, controller: ‘application’, action: ‘hello’
# ———————– with match —————-
match ‘hello’ => ‘application#hello’, via: :get
match ‘hello’, to: ‘application#hello’, via: :get
match ‘hello’, controller: ‘application’, action: ‘hello’, via: :get
How to add dynamic params to route?
get 'hello/:id', controller: 'application', action: 'hello' # params[:id]
How to send default segment params( keys)?
get 'hello/:id', to: 'application#hello', defaults: {something: 'hi'} # params[:something]
How to redirect?
get ‘hello’, to: redirect(‘/’)
How to add segment key for format to the request?
get 'hello/:id(.:format)', to: 'application#hello' # params[:format]
How to route action without a controller?
get 'hello', to: proc { |env| [200, {}, ["Hello world"]] } # => 'Hello world'
How to set constraints for segment key?(2 ways)
get ‘hello/:id’, to: ‘application#hello’, constraints: { id: /\d+/}
get ‘hello/:id’, to: ‘application#hello’, id: /\d+/
How to add root action? (2 ways)
root to: ‘application#hello’
root ‘application#hello’
How to add globbing values to the route?
get 'hello/*something' => 'application#hello' # params[something] #=> one/two/some
How to add a custom name for path?
get 'hello' => 'application#hello', as: :my_hello # my_hello_path
What is the difference between name_path and name_url?
The difference is that the _url method generates an entire URL, including protocol and domain, whereas the _path method generates just the path part (sometimes referred to as an absolute path or a relative URL).
How to create a scope?
scope ‘sample’ do
get ‘hello’ => ‘application#hello’
end
# sample/hello
How to use a common controller in a scope? ( 2 way )
scope 'sample', controller: 'application' do get 'hello', action: :hello end # sample/hello # ---------------- controller 'application' do get 'hello', action: :hello end
How to add namespace?
namespace :application do
get ‘hello’, action: :hello
end
How to add direct url?
direct(:apple) { "http://www.apple.com" } # apple_url #=> http://www.apple.com
How to see the whole info about the route?
You can visit http://localhost:3000/rails/info/routes
How to generate resources for controller?
resources :articles
Which routes generates resources?
resources :articles
get ‘articles’, controller: ‘articles’, action: ‘index’
get ‘articles/:id’, controller: ‘articles’, action: ‘show’
get ‘articles/new’, controller: ‘articles’, action: ‘new’
post ‘articles’, controller: ‘articles’, action: ‘create’
get ‘articles/:id/edit’, controller: ‘articles’, action: ‘edit’
put ‘articles/:id’, controller: ‘articles’, action: ‘update’
delete ‘articles/:id’, controller: ‘articles’, action: ‘destroy’
How to use route_path with params?
get ‘articles/:id/:id2’, controller: ‘articles’, action: ‘show’, as: :sample
sample_path(1,2) # => articles/1/2
How to use only specific actions in the resources? ( 2 ways )
resources :articles, only: [:index]
resources :articles, except: [:index]
What are the differences between resources and resource?
resource :article
Works only for one action, the same action but without index and :id ( article/new )
How does resource inside another resource works?
resources :articles do
resources :comments
end
take articles/:id/ and add ‘comments’ resources routes to him
Can you tell me what paths resources generates? ( 4 ways )
resources :articles
articles_path
new_article_path
edit_article_path
article_path
Can you tell me what paths resources inside resources generates? ( 4 ways )
resources :articles do
resources :comments
end
the same as usual route but with a singular form
article_comments
new_article_comment_path
edit_article_comment_path
How to use concern in routes?
concern :comment_resource do
resources :comments
end
resources :articles, concerns: [:comment_resource]
What are member and collection method in route?
resources :articles do member do # articles/1/my_new_collection get :my_new end
collection do # articles/my_new_collection get :my_new_collection end end
How to change new/edit name from resources?
resources :articles, path_names: { new: 'new2', edit: 'edit2' } # articles/new2
How to use another controller for the resources?
resources :articles, controller: ‘application’