ruby-on-rails – 在Rails路由中使用“to:”和fat-arrow“=>
发布时间:2020-12-16 20:22:00 所属栏目:百科 来源:网络整理
导读:在 RailsGuides routing tutorial中,他们给出了以下例子 如何使用hash参数设置一个简单的路由: get '/patients/:id',to: 'patients#show' 但是当您生成一个新的Rails应用程序(使用Rails 4.0.3) rails new命令,生成的config / routes.rb文件给出以下内容 一
在
RailsGuides routing tutorial中,他们给出了以下例子
如何使用hash参数设置一个简单的路由: get '/patients/:id',to: 'patients#show' 但是当您生成一个新的Rails应用程序(使用Rails 4.0.3) get 'products/:id' => 'catalog#view' 这些不同的方法之间是否有区别,以定义一条路线,或者 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)的实际来源 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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |