【问题标题】:Rails: Is there a problem using get 'signup' => 'users#new' instead of get 'signup', to: 'users#new'Rails:使用 get 'signup' => 'users#new' 而不是 get 'signup' 是否有问题:'users#new'
【发布时间】:2020-04-15 21:50:58
【问题描述】:

使用有问题吗

获取“注册”=>“用户#索引”

而不是

get 'signup', to: 'users#index'?

【问题讨论】:

    标签: ruby-on-rails routes


    【解决方案1】:

    TL;DR

    没有。


    并非如此,get 方法已准备好处理其中任何一个。

    # File actionpack/lib/action_dispatch/routing/mapper.rb, line 711
    def get(*args, &block)
      map_method(:get, args, &block)
    end
    
    # File actionpack/lib/action_dispatch/routing/mapper.rb, line 748
    def map_method(method, args, &block)
      options = args.extract_options!
      options[:via] = method
      match(*args, options, &block)
      self
    end
    
    # File actionpack/lib/action_dispatch/routing/mapper.rb, line 1597
    #
    # Matches a URL pattern to one or more routes.
    # For more information, see match[rdoc-ref:Base#match].
    #
    #   match 'path' => 'controller#action', via: :patch
    #   match 'path', to: 'controller#action', via: :post
    #   match 'path', 'otherpath', on: :member, via: :get
    def match(path, *rest, &block)
    

    在一天结束时,match 被调用,如果你去查看它的实现,它有正确处理它的代码。

    另外,The Rails Style Guide 对此没有任何建议。

    【讨论】:

      【解决方案2】:

      这两种方式都不应该有问题。 =>,称为hashrocket,是, to: 的简写。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多