默认路由:
# Rails3:
match '/:controller(/:action(/:id))'
# Rails2:
map.connect ':controller/:action/:id'
正则路由:
# Rails3:
match 'products/:id', :to => 'catalog#view'
# Rails2:
map.connect 'products/:id', :controller => 'catalog', :action => 'view'
命名路由:
# Rails3:
match 'logout', :to => 'sessions#destroy', :as => 'logout'
# Rails2:
map.logout 'logout', :controller => 'sessions', :action => ''
根路由:
# Rails3:
root :to => 'welcome#show'
# Rails2:
map.root :controller => 'welcome', :action => 'show'
路由简写技巧:
:to 键的省略:
match 'account' => 'account#index'
# 相当于:
match 'account', :to => 'account#index'
match 'info' => 'projects#info', :as => 'info'
注意:
:as 在rails3中是改变 helper, 在rails2中是改变 path
当路径和控制器(及action)一至时,可省略指派控制器部分
match 'account/overview'
# 相当于:
match 'account/overview', :to => 'account#overview'
Verb路由
当需要限制http请求方法的时候通过键
:via ,也可以直接把方法写在最前面:
get 'account/overview'
# 相当于:
match 'account/overview', :via => 'get'
match 'account/setup', :via => [:get, :post]
# 支持get\post\put\delete四种HTTP方法
resources路由:
resources :posts, :except => [:index]
resources :posts, :only => [:new, :create]
# edit_post GET /posts/:id/modify(.:format) {:controller=>"posts", :action=>"edit"}
resources :posts, :path_names => { :edit => 'modify' }
resources :projects do
resources :tasks, :people
end
resources :products do
collection do
get :sold
post :on_offer, :search
end
get :buy, :on => :member
post :batch, :on => :collection
end
resource :session do
get :create
end
:shallow用法:
Rails3中的shallow用法与Rails2中一致
resources :blogs, :shallow => true do
resources :comments
end
使用:shallow前后相同部分:
blog_comments | GET | /blogs/:blog_id/comments(.:format) | {:controller=>"comments", :action=>"index"} |
blog_comments | POST | /blogs/:blog_id/comments(.:format) | {:controller=>"comments", :action=>"create"} |
new_blog_comment | GET | /blogs/:blog_id/comments/new(.:format) | {:controller=>"comments", :action=>"new"} |
blogs | GET | /blogs(.:format) | {:controller=>"blogs", :action=>"index"} |
blogs | POST | /blogs(.:format) | {:controller=>"blogs", :action=>"create"} |
new_blog | GET | /blogs/new(.:format) | {:controller=>"blogs", :action=>"new"} |
edit_blog | GET | /blogs/:id/edit(.:format) | {:controller=>"blogs", :action=>"edit"} |
blog | GET | /blogs/:id(.:format) | {:controller=>"blogs", :action=>"show"} |
blog | PUT | /blogs/:id(.:format) | {:controller=>"blogs", :action=>"update"} |
blog | DELETE | /blogs/:id(.:format) | {:controller=>"blogs", :action=>"destroy"} | |
使用:shallow前后不同部分:
不使用shallow选项:
edit_blog_comment | GET | /blogs/:blog_id/comments/:id/edit(.:format) | {:controller=>"comments", :action=>"edit"} |
blog_comment | GET | /blogs/:blog_id/comments/:id(.:format) | {:controller=>"comments", :action=>"show"} |
blog_comment | PUT | /blogs/:blog_id/comments/:id(.:format) | {:controller=>"comments", :action=>"update"} |
blog_comment | DELETE | /blogs/:blog_id/comments/:id(.:format) | {:controller=>"comments", :action=>"destroy"} |
使用shallow选项后:
edit_comment | GET | /comments/:id/edit(.:format) | {:controller=>"comments", :action=>"edit"} |
comment | GET | /comments/:id(.:format) | {:controller=>"comments", :action=>"show"} |
comment | PUT | /comments/:id(.:format) | {:controller=>"comments", :action=>"update"} |
comment | DELETE | /comments/:id(.:format) | {:controller=>"comments", :action=>"destroy"} |
可以看出使用shallow选项后,对于已经存在的资源使用简化方式操作,具体行为涉及到 edit\show\update\destroy 四种
另外,shallow选项的有效范围是对自身及嵌套的资源都有效,如下面这个例子:
resources :publishers do
resources :magazines do
resources :albums, :shallow => true do
resources :photos do
resources :images
end
end
end
end
这个例子中 albums、photos、images 都会使用简化方式,而 magazines 不会。特别注意:这种嵌套方式极不推荐,一般嵌套的层级最好不要超过一级
scope路由
:path 改变Path,:module 改变Controller, :name_prefix || :as 改变 helper
scope 'admin' do
resources :posts
end
# 行当于:
scope :path => 'admin' do
resources :posts
end
生成路由:
posts | GET | /admin/posts(.:format) | {:controller=>"posts", :action=>"index"} |
posts | POST | /admin/posts(.:format) | {:controller=>"posts", :action=>"create"} |
new_post | GET | /admin/posts/new(.:format) | {:controller=>"posts", :action=>"new"} |
edit_post | GET | /admin/posts/:id/edit(.:format) | {:controller=>"posts", :action=>"edit"} |
post | GET | /admin/posts/:id(.:format) | {:controller=>"posts", :action=>"show"} |
post | PUT | /admin/posts/:id(.:format) | {:controller=>"posts", :action=>"update"} |
post | DELETE | /admin/posts/:id(.:format) | {:controller=>"posts", :action=>"destroy"} |
scope :module => 'admin' do
resources :posts
end
# 相当于:
resources :posts, :module => 'admin'
生成路由:
posts | GET | /posts(.:format) | {:controller=>"admin/posts", :action=>"index"} |
posts | POST | /posts(.:format) | {:controller=>"admin/posts", :action=>"create"} |
new_post | GET | /posts/new(.:format) | {:controller=>"admin/posts", :action=>"new"} |
edit_post | GET | /posts/:id/edit(.:format) | {:controller=>"admin/posts", :action=>"edit"} |
post | GET | /posts/:id(.:format) | {:controller=>"admin/posts", :action=>"show"} |
post | PUT | /posts/:id(.:format) | {:controller=>"admin/posts", :action=>"update"} |
post | DELETE | /posts/:id(.:format) | {:controller=>"admin/posts", :action=>"destroy"} |
scope :name_prefix => 'admin' do
resources :posts
end
# 相当于:
resources :posts, :name_prefix => 'admin'
生成路由:
admin_posts | GET | /posts(.:format) | {:controller=>"posts", :action=>"index"} |
admin_posts | POST | /posts(.:format) | {:controller=>"posts", :action=>"create"} |
new_admin_post | GET | /posts/new(.:format) | {:controller=>"posts", :action=>"new"} |
edit_admin_post | GET | /posts/:id/edit(.:format) | {:controller=>"posts", :action=>"edit"} |
admin_post | GET | /posts/:id(.:format) | {:controller=>"posts", :action=>"show"} |
admin_post | PUT | /posts/:id(.:format) | {:controller=>"posts", :action=>"update"} |
admin_post | DELETE | /posts/:id(.:format) | {:controller=>"posts", :action=>"destroy"} |
scope 'admin', :module => 'admin', :name_prefix => 'admin' do
resources :posts
end
# 相当于:
namespace 'admin' do
resources :posts
end
生成路由:
admin_posts | GET | /admin/posts(.:format) | {:controller=>"admin/posts", :action=>"index"} |
admin_posts | POST | /admin/posts(.:format) | {:controller=>"admin/posts", :action=>"create"} |
new_admin_post | GET | /admin/posts/new(.:format) | {:controller=>"admin/posts", :action=>"new"} |
edit_admin_post | GET | /admin/posts/:id/edit(.:format) | {:controller=>"admin/posts", :action=>"edit"} |
admin_post | GET | /admin/posts/:id(.:format) | {:controller=>"admin/posts", :action=>"show"} |
admin_post | PUT | /admin/posts/:id(.:format) | {:controller=>"admin/posts", :action=>"update"} |
admin_post | DELETE | /admin/posts/:id(.:format) | {:controller=>"admin/posts", :action=>"destroy"} |
在路由中定义跳转:
match "/posts/github" => redirect("http://github.com/rails.atom")
# 地址 /foo/1 会自动跳转到 /bar/1s
match "/foo/:id", :to => redirect("/bar/%{id}s")
# /account/proc/inosin 会自动跳转到 /inosins
match 'account/proc/:name', :to => redirect {|params|
"/#{params[:name].pluralize}" }
match "/stories" => redirect {|p, req| "/posts/#{req.subdomain}" }
路由中的限制:
# 限制 id 只能为数字
match "/posts/show/:id", :to => "posts#index", :id => /\d+/
match "/posts/show/:id", :to => "posts#index", :constraints => {:id => /\d+/}
# 限制子域名
match "photos", :constraints => {:subdomain => "admin"}
# 限制访问者 IP
constraints(:ip => /127.0.0.1/) do
match '/questions', :to => redirect("http://www.stackoverflow.com/")
end
# 当访问者 ip 是 192.168.1.* 的来访者访问 子域名为 "test"
match "/ttt" => proc{|env| [200, {}, ["hello test"]]}, \
:constraints => {:subdomain => "test", :ip => /192\.168\.1\.\d+/}
路由通配符:
resources :photos, :id => /\d+/
match 'photos/*other' => 'photos#unknown'
#上面这两行路由则会把不符合7种path的其他url全部解析到PhotoController#unknown中去处理,params[:other]可得到path中/photos/之后的部分,注意这两行的顺序不能颠倒
match 'books/*section/:title' => 'books#show'
# 例如:books/some/section/last-words-a-memoir 中 params[:section] = "some/section", params[:title] = "last-words-a-memoir".
match '*a/foo/*b' => 'test#index'
# 例如:zoo/woo/foo/bar/baz 中 params[:a] = "zoo/woo", params[:b] = "bar/baz"
Rack:
match "/foo", :to => proc {|env| [200, {}, ["Hello world"]] }
match 'rocketeer.js' => ::TestRoutingMapper::RocketeerApp
RocketeerApp = lambda { |env|
[200, {"Content-Type" => "text/html"}, ["javascripts"]]
}
参考文档:
http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/
http://www.railsdispatch.com/posts/rails-routing
http://guides.rails.info/routing.html
http://asciicasts.com/episodes/203-routing-in-rails-3
http://asciicasts.com/episodes/231-routing-walkthrough
http://asciicasts.com/episodes/232-routing-walkthrough-part-2
分享到:
相关推荐
在Rails应用程序的根文件夹中,从命令行运行rails-route-checker 。 您也可以使用-c或--config标志来指定自定义配置文件。 默认情况下,该配置文件是在.rails-route-checker.yml 。 可以在下面找到有关配置文件的更...
- **方法**:在控制器中使用`render`方法来显示特定的视图文件。 - **视图文件**:通常使用ERB模板语言来编写视图文件,这些文件位于`app/views`目录下。 #### 八、组件 - **定义**:组件是可重用的代码块,用于...
写一个宝石描述安装将此行添加到应用程序的 Gemfile 中: gem 'route_list'然后执行: $ bundle或者自己安装: $ gem install route_list用法 RouteList::Route.list #returns route hash of current rails ...
- **路由(Route)**:Rails使用声明式的路由方式,使得URL到控制器和动作之间的映射变得清晰易懂。 - **ActiveRecord**:Rails中的ORM(对象关系映射)组件,它简化了数据库操作,使开发者可以更专注于业务逻辑而非...
在Rails中,资源通常指的是模型(Model)、路由(Route)、控制器(Controller)和视图(View)等核心组件,它们共同构成了应用程序的主要结构。 1. **Rails安装**:Rails的安装需要Ruby环境的支持,首先需要确保已...
Rails . application . config . middleware . use RouteCounter :: Middleware 添加到 Rakefile: require "route_counter/tasks" 用法 检查使用了什么。 $ bundle exec rake routes:count:local 所有这些都...
3. **基本用法** 使用`route-pattern`创建路由并进行匹配非常直观。首先,你需要创建一个路由模式对象,然后调用其`match`方法传入URL字符串进行匹配。例如: ```javascript const RoutePattern = require('...
类似于Rails的url帮助器, rails.macro为每个已定义的路由提供方法,即<routeName>_path和<routeName>_url 。 您可以从导出的Routes对象中调用它们。 基本用法 给定Rails路线my_cool_thing config / routes.rb ...
Gem具有类似于Rails Route,Factory bot的语法,并使用造假者生成伪数据。 特征: 快速API原型清晰而熟悉的语法Faker用于测试数据生成包括图像链接(如果需要) 管理Cookie,会话,带有虚假响应的标题实时执行您的...
Vapor中的路由定义通常涉及`Route`结构体,通过`app.get()`, `app.post()`, `app.put()`, `app.delete()`等方法来创建。然而,对于需要执行CRUD操作的开发者来说,手动为每个操作编写路由可能会变得繁琐。这就是...
rails g webhookr:add_route 或者,将路由信息手动添加到config / routes.rb mount Webhookr :: Engine => "/webhookr" , :as => "webhookr" 然后运行生成器以将代码添加到初始化程序。 如果您没有一个初始化器...
人群::观察者 Crowd Spotter 监控的性能。 它定期收集有关作业待处理/处理/... 或者使用 route.rb 安装到 Rails 应用程序中: 要求 'crowd/spotter' Rails.application.routes.draw 挂载 Crowd::Spotter::Engine, at:
3. **Route组件**:用于定义路由,每个`<Route>`组件对应一个URL路径,当路径匹配时,对应的组件会被渲染。 4. **Link组件**:用于创建链接,点击时触发导航,而不会导致页面刷新。 5. **Switch组件**:用于包裹多...
用法作为RAILS插件创建config / log-monitor.yml monitor: target: /tmp/log/development.log words: - Completed 500 Internal Server Error - No route matchesmethod: file # ,console, email or webpostfile: /...
5. **路由和导航**:学习如何在Flutter应用中进行页面间的跳转和数据传递,使用Navigator和Route。 6. **网络请求**:掌握使用Dart的http库进行HTTP请求,或者使用dio库进行更高级的网络交互。 7. **数据持久化**...
使用`Route::apiResource`可以轻松创建RESTful API,例如: ```php Route::apiResource('users', 'UserController'); ``` 这将为用户资源自动设置CRUD相关的路由。 四、控制器 在Laravel中,控制器是处理请求和...
Encryptbot在Heroku上创建并更新您的Let's Encrypt SSL证书,允许使用多个通配符。 宝石将: 创建让我们加密 将Let's Encrypt DNS Challenge TXT记录添加到DNS提供商Route 53 将证书添加到您的Heroku SNI端点 ...
通过`Route::get()`, `Route::post()`等方法可以轻松定义路由规则。 4. **Blade模板引擎**:Blade是Laravel内置的轻量级模板引擎,它提供了简洁的语法,可以方便地进行视图的组合和继承。Blade支持条件语句、循环...
best_boy 一个简单的事件驱动的ActiveRecord模型日志记录。...boy配置文件: rails g best_boy:install用法在模型上下文中: include BestBoy::Eventablehas_a_best_boy这将记录每个实例的“创建”和“删除”事件。
4. **路由(Route)**:路由是Jfina中连接URL与Controller的方法的关键。开发者可以通过`config.initWithConfigFile()`配置路由规则,实现URL的灵活映射。 5. **插件(Plugin)**:Jfina 提供了一套强大的插件机制...