美文网首页
Rails路由学习

Rails路由学习

作者: yaya_pangdun | 来源:发表于2016-06-14 21:24 被阅读55次

一般路由

get  'meetings/:id', :to => 'events#show'
post 'meetings', :to =>'events#create'

events_controller的show和create action
简写

get 'meetings/:id' => 'events#show'

其中:id会被转车一个参数params[:id]传到Controller里。

match路由

match  ':controller(/:action(/:id(.:format)))', :via => :all

括号内的内容表示可有可无的内容

match '/:controller', via: :all
match '/:controller/:action', via: :all
match '/:controller/:action/:id', via: :all
match '/:controller.:format', via: :all
match '/:controller/:action.:format', via: :all
match '/:controller/:action/:id.:format', via: :all
match '/test_match/:action'=> 'test_match#:action' ,:via => :all

.format让路由可以接受.json、.xml参数,并且转成params[:format]参数中。

命名路由

get '/meetings' => 'events#index', :as => 'meetings'

其中的:as部分会产生meetings_path和meeting_url的Helpers,_path是相对路径,_url是绝对路径。

路由重定向

get '/foo' => redirect('/bar')
get "/ihower" => redirect("http://ihower.tw")

设置首页

root :to => 'welcome#show'

限定动词

match 'account/overview' => 'account#overview', :via=> :get
match 'account/setup' => 'account#setup', :via => [:get, :post]
match 'account/overview' => 'account#overview', :via => :all

scope规则

scope :controller => 'events', :path => '/foo', :as=>'bar' do
  get 'meetings/:id' => :show, :as =>'meeting'
  post 'meetings' => :create, :as=>'meerings'
end

产生bar_meeting_url和bar_meetings_url。

Namespace

namespace :admin do
  resources :projects
end

controller对应Admin::ProjectsController,网址为/admin/projects,而URL Helper如admin_projects_path。
每个Namespace都可以设置首页

namespace :admin do
  root 'projects#index'
end

这样连接/admin的时候就默认使用ProjectsController的index方法。

限定条件

限制:id是整数

match '/events/show/:id' => 'events#show', :constraints => {:id => /\d/}

甚至可以限定IP位置

constraints(:ip => /(^127.0.0.1$)|(^192.168.[0-9]{1,3}.[0-9]{1,3}$)/) do
    match "/events/show/:id" => "events#show"
end

自定义集合路由

resources :products do
  collection do
    get :sold
    post :on_offer
  end
end

产生sold_products_path和on_offer_products_path这两个URL Helper,产生出如products/sold和products/on_off

限定路由

resources :events, :except => [:index, :show]
resources :events, :only => :create

相关文章

  • rails项目学习

    Rails路由学习 <%= link_to 'hello', {:controller => 'welcome',...

  • rails基础汇总

    一:ruby on rails, Router路由 前言:ruby学习掌握20%就可以开始rails的学习了,知道...

  • Rails路由学习

    一般路由 events_controller的show和create action简写 其中:id会被转车一个参数...

  • rails 方法和对象

    rails 路由(rails 通过路由配置表来转发url动作) 路由表的功能: 接收识别http 请求 处理url...

  • rails 路由学习1

    rails config/routes.rb 指定路由的代码如下 上面的代码定义了5种风格的路由,rails在启动...

  • Rails 路由学习笔记

    参考 RailsGuides中的Rails Routing from the Outside In 简介 Rail...

  • Routes 路由

    Rails路由种类 (一般路由,命名路由) 使用 :except排除某个路由resource :posts, :e...

  • Rails路由

    资源路由 一行代码完成资源资源路由声明: 这会创建7个不同的路由,这些路由会映射到 Photos 控制器上。 这样...

  • rails 环境配置

    ruby 的各种概念 环境配置 工具配置 rails路由配置 (routes.rb) rails 命令 实例如下:...

  • 2018-08-14-资源式路由

    在rails 中,资源路由应该是rails的一大特色,为方便记忆,画如下图:

网友评论

      本文标题:Rails路由学习

      本文链接:https://www.haomeiwen.com/subject/mrpydttx.html