routing 1, season 1
TRANSCRIPT
Rails Routing from the Outside In
(1)
The 13th Round of ROR Lab.
May 26th, 2012
Hyoseong ChoiROR Lab.
ROR Lab.
Rails Router
http://blog.org
A Controller Action
_path _url
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
ROR Lab.
An Incoming Request
?GET /posts/17
ROR Lab.
Generating paths & URLs
@patient = Patient.find(17)
<%= link_to "Patient Record", patient_path(@patient) %>
URL = host + port + PATH
ROR Lab.
Resource Routing
• Action 7 brothers
• index / show / new / editcreate / update / destroy
resources :photos
in config/routes.rb
“sine” - show, index, new, edit
ROR Lab.
An Incoming Request
?DELETE /posts/17
ROR Lab.
HTTP verbs
• POST
• GET
• PUT
• DELETE
C
R
U
D
new create
index show
update
destroy
URL
Note : routing orders
ROR Lab.
Multiple Resources
resources :photos, :books, :videos
resources :photosresources :booksresources :videos
ROR Lab.
Singular Resources
match "profile" => "users#show"
resource :geocoder
ROR Lab.
Singular Resources
resource :geocoder
Action 6 brothers no index action
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
ROR Lab.
Controller Namespacing
namespace :admin do resources :posts, :comments
Admin:: module prefix
ROR Lab.
Controller Namespacing
scope :module => "admin" do resources :posts, :comments
Admin:: module prefix
ROR Lab.
Controller Namespacing
scope "/admin" do resources :posts, :comments
without Admin:: module prefix
ROR Lab.
Nested Resources
class Magazine < ActiveRecord::Base has_many :adsend class Ad < ActiveRecord::Base belongs_to :magazine
resources :magazines do resources :ads
ROR Lab.
Nested Resourcesresources :magazines do resources :ads
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) %>
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
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
ROR Lab.
감사합니다.����������� ������������������