Rails Router follow-up
In 4 cases
routes.rb
# Example resource route within a namespace: namespace :admin do # Directs /admin/test/* to Admin::ProductsController # (app/controllers/admin/test_controller.rb) resources :test endWatch carefully
# Directs /admin/test/* to Admin::TestController # (app/controllers/admin/test_controller.rb)Create a directory admin on controllers, a new test_controller.rb in admin. Test_controller.rb specific code to see:
# What the controller on Admin::
class Admin::TestController <ApplicationController end
URL
http://ip:host/admin/test
The table below details. The resources table with the same, but the front with a admin
HTTP Verb | Path | action |
---|---|---|
GET | /admin/test | index |
GET | /admin/test/new | new |
POST | /admin/test | create |
GET | /admin/test/:id | show |
GET | /admin/test/:id/edit | edit |
PATCH/PUT | /admin/test/:id | update |
DELETE | /admin/test/:id | destroy |
If you want to route /test (not /admin prefix) matched to Admin:: TestController, you need it:
scope :module => "admin" do resources :test, :comments end
Or when matching on a single Resource
resources :test, :module => "admin"
The URL into
http://ip:host/Test can also access to TestController Action
But if you want to: /admin/test routing to match to TestController (no Admin:: controller this module as a prefix), you can use
scope "/admin" do resources :test endOr when the matching for a single Resource wrote
resources :test, :path => "/admin/test"
In 5 cases
Resources nested together, individuals will not be used in the actual development, this first pass.
resources :publishers do resources :magazines do resources :photos end endThe above several commonly used, is also common, first to some. Below is a list of a few magic usage.
This route will match like /photos/A12345 routing. You can use this more concise way to write the rules:
match 'photos/:id' => 'photos#show', :id => /[A-Z]\d{5}/Redirection
match "/stories" => redirect("/posts")You can also put the dynamic part of the error handling in a match command (rescue) to redirect
match "/stories/:name" => redirect("/posts/%{name}"):The controller option allows you to select the associated with the resource controller. For example:
resources :photos, :controller => "images"You can use the constraints option to define: ID format. For example:
resources :photos, :constraints => {:id => /[A-Z][A-Z][0-9]+/}This statement limits: ID must match the regularization parameter. So, in this example, the route will no longer match /photos/1, and only /photos/RR27 that will be matched. You can use a code block to a single canonical restrictions applied to multiple rules.
constraints(:id => /[A-Z][A-Z][0-9]+/) do resources :photos resources :accounts end
The above according to some online resources and their own understanding, summary. There are parts of the interior is the original copy. Thanks contributors!
Posted by Anastasia at February 16, 2014 - 5:00 AM