【问题标题】:Rails routes - scope regex or redirectionRails 路线 - 范围正则表达式或重定向
【发布时间】:2014-02-18 15:14:10
【问题描述】:

当我的项目与第三方项目集成时,会出现严重的冲突。

我使用模型复数作为路线名称,例如:

http://my_app/users/search (plural)

在他们使用时

http://my_app/user/search (singular)

这种模式用于分布在 4 个模型中的 19 条不同的路线,所以我真的不想为了支持另一个项目而复制每条路线。

有没有办法在范围上使用正则表达式或重定向以避免代码重复?

目前我有:

...
scope 'users' do
  get 'search'
  scope 'id' do
    get ''
    get 'ping'
  end
end
...

我希望得到类似的东西:

...
scope 'user(s)?' do
  get 'search'
  scope 'id' do
    get ''
    get 'ping'
  end
end

或者:

scope 'user', to: redirect('users')

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 routes


    【解决方案1】:
    scope ':pattern', constraints: { pattern: /user(s)?/ } do
      ...
    end
    

    【讨论】:

    • 不用括号,用/users?/regexp即可
    猜你喜欢
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 2016-04-07
    相关资源
    最近更新 更多