ruby Rails获取控制器操作url

qq24tv8q  于 2023-01-12  发布在  Ruby
关注(0)|答案(2)|浏览(161)

我有一个操作,我已经在routes.rb中定义了它

resources :statistics, only: [] do
  get 'group_report/:id', to: 'statistics#group_report', as: 'group_report'
end

然后在模型的方法中,我想返回完整的URL。
因此,我编写了这个方法

root_url + group_report_statistics_path(id)

结果为"http://localhost:3000//statistics/group_report/DHo6qzUzYXw"
我认为这是一个肮脏的方式,我需要处理双斜线以及。
Rails是否提供了一些有用的方法,让我可以直接获得操作的完整URL?

qxsslcnc

qxsslcnc1#

Rails为所有路由提供了path和url帮助器。
因此,如果您使用group_report_statistics_url(id),您将获得完整的URL。
有关详细说明,请参见手册的“Rails”部分中的路径和URL帮助器。

z0qdvdin

z0qdvdin2#

试试这个

File.join(root_url + group_report_statistics_path(id))

group_report_statistics_url(id)

相关问题