Routing Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

How to add ‘/hello’ get action?(7 ways)

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to add dynamic params to route?

A
get 'hello/:id', controller: 'application', action: 'hello'
# params[:id]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to send default segment params( keys)?

A
get 'hello/:id', to: 'application#hello', defaults: {something: 'hi'}
# params[:something]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to redirect?

A

get ‘hello’, to: redirect(‘/’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to add segment key for format to the request?

A
get 'hello/:id(.:format)', to: 'application#hello'
# params[:format]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to route action without a controller?

A
get 'hello', to: proc { |env| [200, {}, ["Hello world"]] }
# => 'Hello world'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to set constraints for segment key?(2 ways)

A

get ‘hello/:id’, to: ‘application#hello’, constraints: { id: /\d+/}
get ‘hello/:id’, to: ‘application#hello’, id: /\d+/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to add root action? (2 ways)

A

root to: ‘application#hello’

root ‘application#hello’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to add globbing values to the route?

A
get 'hello/*something' => 'application#hello'
# params[something] #=> one/two/some
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to add a custom name for path?

A
get 'hello' => 'application#hello', as: :my_hello
# my_hello_path
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the difference between name_path and name_url?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to create a scope?

A

scope ‘sample’ do
get ‘hello’ => ‘application#hello’
end
# sample/hello

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to use a common controller in a scope? ( 2 way )

A
scope 'sample', controller: 'application' do
    get 'hello', action: :hello
  end
# sample/hello
# ----------------
  controller 'application' do
    get 'hello', action: :hello
  end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to add namespace?

A

namespace :application do
get ‘hello’, action: :hello
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to add direct url?

A
direct(:apple) { "http://www.apple.com" }
# apple_url #=> http://www.apple.com
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to see the whole info about the route?

A

You can visit http://localhost:3000/rails/info/routes

17
Q

How to generate resources for controller?

A

resources :articles

18
Q

Which routes generates resources?

A

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’

19
Q

How to use route_path with params?

A

get ‘articles/:id/:id2’, controller: ‘articles’, action: ‘show’, as: :sample

sample_path(1,2)
# => articles/1/2
20
Q

How to use only specific actions in the resources? ( 2 ways )

A

resources :articles, only: [:index]

resources :articles, except: [:index]

21
Q

What are the differences between resources and resource?

A

resource :article

Works only for one action, the same action but without index and :id ( article/new )

22
Q

How does resource inside another resource works?

A

resources :articles do
resources :comments
end

take articles/:id/ and add ‘comments’ resources routes to him

23
Q

Can you tell me what paths resources generates? ( 4 ways )

A

resources :articles

articles_path
new_article_path
edit_article_path
article_path

24
Q

Can you tell me what paths resources inside resources generates? ( 4 ways )

A

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

25
Q

How to use concern in routes?

A

concern :comment_resource do
resources :comments
end

resources :articles, concerns: [:comment_resource]

26
Q

What are member and collection method in route?

A
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
27
Q

How to change new/edit name from resources?

A
resources :articles, path_names: { new: 'new2', edit: 'edit2' }
# articles/new2
28
Q

How to use another controller for the resources?

A

resources :articles, controller: ‘application’