【问题标题】:URL params for polymorphic url_for多态 url_for 的 URL 参数
【发布时间】:2012-07-15 21:38:22
【问题描述】:

我目前有这个代码:

link_to 'add a new baz!', new_foo_bar_baz_path(@foo, @bar, baz: { thing: 'the-value' })

生成此网址:

http://ganxy.local/foos/1/bars/2/bazes/new?baz%5Bthing%5D=the-value

由于我正在对我的应用程序进行一些更改,我想让 url 生成多态。所以,是这样的:

link_to 'add a new baz!', [@foo, @bar, :baz]# : { thing: 'the-value' })

有没有办法将?baz%5Bthing%5D=the-value添加到末尾,还是我需要手动生成字符串?

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    我不建议使用多态 url,因为它比辅助方法慢 2 倍。查看my post about this

    【讨论】:

      【解决方案2】:

      Rails 3 - Nested resources and polymorphic paths: OK to two levels, but break at three

      所以你可以写

      link_to 'add a new baz!', polymorphic_url([@foo, @bar, :baz], thing: 'the-value')
      

      【讨论】:

      • 是的,太好了!还有polymorphic_path,正是我需要的。
      【解决方案3】:

      你可以这样做:

      link_to 'add a new baz!', [[@foo, @bar, :baz], thing: 'the-value']
      

      【讨论】:

        猜你喜欢
        • 2022-07-09
        • 2013-07-26
        • 2015-11-21
        • 1970-01-01
        • 2019-03-01
        • 1970-01-01
        • 2011-11-20
        • 1970-01-01
        • 2014-01-15
        相关资源
        最近更新 更多