routing 1, season 1

21
Rails Routing from the Outside In (1) The 13th Round of ROR Lab. May 26th, 2012 Hyoseong Choi ROR Lab.

Upload: rorlab

Post on 29-Jan-2018

1.647 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Routing 1, Season 1

Rails Routing from the Outside In

(1)

The 13th Round of ROR Lab.

May 26th, 2012

Hyoseong ChoiROR Lab.

Page 2: Routing 1, Season 1

ROR Lab.

Rails Router

http://blog.org

A Controller Action

_path _url

Page 3: Routing 1, Season 1

ROR Lab.

$ rake routes

posts GET /posts(.:format) posts#index POST /posts(.:format) posts#create new_post GET /posts/new(.:format) posts#newedit_post GET /posts/:id/edit(.:format) posts#edit post GET /posts/:id(.:format) posts#show PUT /posts/:id(.:format) posts#update DELETE /posts/:id(.:format) posts#destroy

Page 4: Routing 1, Season 1

ROR Lab.

An Incoming Request

?GET /posts/17

Page 5: Routing 1, Season 1

ROR Lab.

Generating paths & URLs

@patient = Patient.find(17)

<%= link_to "Patient Record", patient_path(@patient) %>

URL = host + port + PATH

Page 6: Routing 1, Season 1

ROR Lab.

Resource Routing

• Action 7 brothers

• index / show / new / editcreate / update / destroy

resources :photos

in config/routes.rb

“sine” - show, index, new, edit

Page 7: Routing 1, Season 1

ROR Lab.

An Incoming Request

?DELETE /posts/17

Page 8: Routing 1, Season 1

ROR Lab.

HTTP verbs

• POST

• GET

• PUT

• DELETE

C

R

U

D

new create

index show

update

destroy

URL

Note : routing orders

Page 9: Routing 1, Season 1

ROR Lab.

Multiple Resources

resources :photos, :books, :videos

resources :photosresources :booksresources :videos

Page 10: Routing 1, Season 1

ROR Lab.

Singular Resources

match "profile" => "users#show"

resource :geocoder

Page 11: Routing 1, Season 1

ROR Lab.

Singular Resources

resource :geocoder

Action 6 brothers no index action

Page 12: Routing 1, Season 1

ROR Lab.

Controller Namespacing

namespace :admin do  resources :posts, :comments

scope :module => "admin" do  resources :posts, :comments

scope "/admin" do  resources :posts, :comments

Admin:: module prefix

Page 13: Routing 1, Season 1

ROR Lab.

Controller Namespacing

namespace :admin do  resources :posts, :comments

Admin:: module prefix

Page 14: Routing 1, Season 1

ROR Lab.

Controller Namespacing

scope :module => "admin" do  resources :posts, :comments

Admin:: module prefix

Page 15: Routing 1, Season 1

ROR Lab.

Controller Namespacing

scope "/admin" do  resources :posts, :comments

without Admin:: module prefix

Page 16: Routing 1, Season 1

ROR Lab.

Nested Resources

class Magazine < ActiveRecord::Base  has_many :adsend class Ad < ActiveRecord::Base  belongs_to :magazine

resources :magazines do  resources :ads

Page 17: Routing 1, Season 1

ROR Lab.

Nested Resourcesresources :magazines do  resources :ads

Page 18: Routing 1, Season 1

ROR Lab.

Paths & URLs From Objects

<%= link_to "Ad details", [@magazine, @ad] %>

<%= link_to "Ad details", url_for([@magazine, @ad]) %>

<%= link_to "Ad details", magazine_ad_path(@magazine, @ad) %>

Page 19: Routing 1, Season 1

ROR Lab.

More RESTful Actions

• Member Routes

resources :photos do  member do    get 'preview'  end

URL : /photos/1/preview with GET

Named Routes : preview_photo_url and preview_photo_path

Page 20: Routing 1, Season 1

ROR Lab.

More RESTful Actions

• Collection Routes

resources :photos do  collection do    get 'search'  end

URL : /photos/search with GET

Named Routes : search_photos_url and search_photos_path

Page 21: Routing 1, Season 1

ROR Lab.

감사합니다.����������� ������������������