Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails Route Basic

Rails Route Basic

簡單地介紹了Rails Route

HeChien Hsu

August 21, 2012
Tweet

More Decks by HeChien Hsu

Other Decks in Technology

Transcript

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. scope ‘/admin’ do resources :posts, :comments end # or resources

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

    resources :posts, module: ‘admin’ with /admin without Admin:: ?
  9. 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
  10. 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
  11. class Post < ActiveRecord::Base has_many :comments end class Comment <

    ActiveRecord::Base belongs_to :post end resources :posts do resources :comments end Nested Resources
  12. 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
  13. 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
  14. resources :posts do member do get ‘preview‘ end get ‘draft’,

    on: :collection end GET /posts/:id/preview preview_post_path(:id) Custom Actions