ruby-on-rails – 在Rails路由中使用“to:”和fat-arrow“=>”有什么区别?

RailsGuides routing tutorial中,他们给出了以下例子
如何使用hash参数设置一个简单的路由:
get '/patients/:id',to: 'patients#show'

但是当您生成一个新的Rails应用程序(使用Rails 4.0.3)
rails new命令,生成的config / routes.rb文件给出以下内容
一个简单路由的例子,使用哈希键/值separator =>

get 'products/:id' => 'catalog#view'

这些不同的方法之间是否有区别,以定义一条路线,或者
他们是一样的吗? The Rails documentation字面上说:

match 'path' => 'controller#action'
match 'path',to: 'controller#action'
match 'path','otherpath',on: :member,via: :get

也就是说,这并没有真正解释什么.

解决方法

使用vs =>之间没有任何功能差异定义路由
在Rails.在内部,路由方法实际上是转换路由参数
表格
<method> '<path>' => '<controller>#<action>'

以此形式

<method> '<path>',to: '<controller>#<action>'

代码

这是转换(来自Rails 4.0.4)的实际来源
注意每一个
get,post等路由方法最终通过它的参数
到这个匹配方法(我的意见):

def match(path,*rest)
  # This if-block converts `=>` to `to`.
  if rest.empty? && Hash === path
    options  = path
    # The `find` method will find the first hash key that is a string
    # instead of a symbol,e.g. `'welcome/index' => 'welcome#index'` instead
    # of `to: 'welcome#index'`. By parallel assignment,`path` then becomes
    # the value of the key,and `to` is assigned the value
    # (the controller#action).
    path,to = options.find { |name,_value| name.is_a?(String) }
    # The following two lines finish the conversion of `=>` to `to` by adding
    # `to` to the options hash,while removing the
    # `'welcome/index' => 'welcome#index'` key/value pair from it
    options[:to] = to
    options.delete(path)
    paths = [path]
  else
    options = rest.pop || {}
    paths = [path] + rest
  end
  # More Code...
end

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...