【问题标题】:rails 3, can I create a route that includes a url param?rails 3,我可以创建包含 url 参数的路线吗?
【发布时间】:2011-03-25 17:50:00
【问题描述】:

我们有

match '/m/:id' => 'foo#mobilemethod'

mobilemethod 接受一个 url 参数 ?find=true 将其置于“查找模式”

有没有办法添加一个路由/f/:id,它将使用url参数?find=true调用mobilemethod

(或者,有没有办法让方法知道最初调用了哪个路由,在这种情况下,我可以简单地将 /m 和 /f 映射到同一个方法,然后在方法中检查在 url 上调用了哪个方法)

我试过了

match '/f/:id' => 'foo#mobilemethod?find=true'

match '/f/:id' => 'foo#mobilemethod/:id?find=true'

但出现“未知操作”错误

【问题讨论】:

    标签: ruby-on-rails url routes


    【解决方案1】:

    这应该可以做到这一点:

    match '/f/:id' => 'foo#mobilemethod', :defaults => {:find => true}
    

    【讨论】:

    • 完美,我认为 Rails 会有一些很酷、简单的方法来做到这一点!
    【解决方案2】:
    match '/f/:id' => 'foo#mobilemethod'
                        ^       ^
                 controller    action
    
    
    match '/f/:id/find/:find' => 'foo#mobile_method'
    

    显示参数 id 和查找

    或者你想要一个默认值?

    match '/m/:id' => 'foo#mobilemethod', :defaults => { :find => true }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-14
      • 2011-01-31
      • 2012-02-26
      • 2011-10-10
      • 1970-01-01
      • 1970-01-01
      • 2018-07-23
      相关资源
      最近更新 更多