如何通过指定控制器和操作生成路径?

问题描述 投票:0回答:1

我想从我的控制器或帮助器生成一个新链接。所以我不能使用视图中可用的link_to方法。

我想通过指定控制器和操作来生成路径,而不是使用名称路径。例如:

{action: "index", controller: "accounts", id: "123"}

但是我将上述参数放入哪种方法?我还没有看过Path.new()方法或类似的东西。

ruby-on-rails routes ruby-on-rails-5
1个回答
0
投票
url_for :controller => 'accounts', :action => 'index', :id => 123 

注意:在5.1之前的Rails版本中,默认情况下url_helpers不包含在控制器中。所以你必须自己包括它:

include Rails.application.routes.url_helpers
© www.soinside.com 2019 - 2024. All rights reserved.