Slide 1

Slide 1 text

Rails Route 徐赫謙

Slide 2

Slide 2 text

Rails Route Basic

Slide 3

Slide 3 text

/posts/10 # match ‘/posts/:id’ => ‘posts#show’

Slide 4

Slide 4 text

@post = Post.find(10) redirect_to post_path(@post) redirect_to @post /posts/10

Slide 5

Slide 5 text

RESTful

Slide 6

Slide 6 text

resources :posts HTTP Verb Path action GET /posts index GET /posts/:id show GET /posts/new new GET /posts/:id/edit edit POST /posts create PUT /posts/:id update DELETE /posts/:id destroy

Slide 7

Slide 7 text

resources :posts HTTP Verb Path action GET posts_path index GET post_path(:id) show GET new_post_path new GET edit_post_path(:id) edit POST posts_path create PUT post_path(:id) update DELETE post_path(:id) destroy

Slide 8

Slide 8 text

resources :posts, :comments, :authors resources :posts resources :comments resources :authors Equivalent

Slide 9

Slide 9 text

Singular Resources

Slide 10

Slide 10 text

match ‘profile’ => ‘users#show’

Slide 11

Slide 11 text

resource :record HTTP Verb Path action GET /record show GET /record/new new GET /record/edit edit POST /record create PUT /record update DELETE /record destroy

Slide 12

Slide 12 text

resource :record HTTP Verb Path action GET record_path show GET new_record_path new GET edit_record_path edit POST record_path create PUT record_path update DELETE record_path destroy

Slide 13

Slide 13 text

posts_url # http://host:1234/posts record_url # http://host:4321/record Include host, port, prefix

Slide 14

Slide 14 text

namespace :admin do resources :posts, :comments end Namespace

Slide 15

Slide 15 text

class Admin::PostController < ApplicationController end For

Slide 16

Slide 16 text

Namespace HTTP Verb Path action GET /admin/posts index GET /admin/posts/:id show GET /admin/posts/new new GET /admin/posts/:id/edit edit POST /admin/posts create PUT /admin/posts/:id update DELETE /admin/posts/:id destroy

Slide 17

Slide 17 text

Namespace HTTP Verb Path action GET admin_posts_path index GET admin_post_path(:id) show GET new_admin_post_path new GET edit_admin_post_path(:id) edit POST admin_posts_path create PUT admin_post_path(:id) update DELETE admin_post_path(:id) destroy

Slide 18

Slide 18 text

class Admin::PostController < ApplicationController end For

Slide 19

Slide 19 text

scope ‘/admin’ do resources :posts, :comments end # or resources :posts, path: ‘/admin/posts’ without /admin ?

Slide 20

Slide 20 text

scope module: ‘admin’ do resources :posts, :comments end # or resources :posts, module: ‘admin’ with /admin without Admin:: ?

Slide 21

Slide 21 text

class PostsController < ApplicationController end For

Slide 22

Slide 22 text

Path HTTP Verb Path action GET /admin/posts index GET /admin/posts/:id show GET /admin/posts/new new GET /admin/posts/:id/edit edit POST /admin/posts create PUT /admin/posts/:id update DELETE /admin/posts/:id destroy

Slide 23

Slide 23 text

Named Helper HTTP Verb Path action GET posts_path index GET post_path(:id) show GET new_posts_path new GET edit_posts_path(:id) edit POST posts_path create PUT post_path(:id) update DELETE post_path(:id) destroy

Slide 24

Slide 24 text

class Post < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :post end resources :posts do resources :comments end Nested Resources

Slide 25

Slide 25 text

Path for Comments HTTP Verb Path action GET /posts/:post_id/comments index GET /posts/:post_id/comments/:id show GET /posts/:post_id/comments/new new GET /posts/:post_id/comments/:id/edit edit POST /posts/:post_id/comments create PUT /posts/:post_id/comments/:id update DELETE /posts/:post_id/comments/:id destroy

Slide 26

Slide 26 text

Helper for Comments HTTP Verb Path action GET post_comments_path(:post_id) index GET post_comment_path(:post_id, :id) show GET new_post_comment_path(:post_id) new GET edit_post_comment_path(:post_id) edit POST post_comments_path(:post_id) create PUT post_comment_path(:post_id, :id) update DELETE post_comment_path(:post_id, :id) destroy

Slide 27

Slide 27 text

redirect_to post_comment_path(@post, @comment) redirect_to url_for(@post, @comment) redirect_to [@post, @comment] Shortcut

Slide 28

Slide 28 text

resources :posts do member do get ‘preview‘ end get ‘draft’, on: :collection end GET /posts/:id/preview preview_post_path(:id) Custom Actions