【问题标题】:How to use url_for to generate a path that doesn't exist in routes如何使用 url_for 生成路由中不存在的路径
【发布时间】:2019-04-01 10:17:09
【问题描述】:

这可能是不可能的,但我想我会问。我需要生成路径文件中不存在的路径。

例如我希望能够生成:

include Rails.application.routes.url_helpers
url_for(
  {
    :controller => 'accounts',
    :action     => 'index',
    :id         => '123',
    :only_path  => true
  }
) # => "/accounts?id=123"

以上将引发ActionController::UrlGenerationError: No route matches

【问题讨论】:

  • 请显示您的rails routes 输出
  • 你可以在你的路由中传递额外的参数,只是不要称之为 id。例如以下路线将转到索引操作,如果您有ressources accountsaccounts_path(other_id: '123') => /accounts?other_id='123'(我不是在我的电脑前检查错别字,但你明白这一点:) )

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


【解决方案1】:

你不能那样做 AFAIK。 url_for 从路由映射中推断出很多东西,生成的 url 无论如何都不起作用。

用例是什么?也许有更好的解决方案。

【讨论】:

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