我有一个操作,我已经在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?
2条答案
按热度按时间qxsslcnc1#
Rails为所有路由提供了path和url帮助器。
因此,如果您使用
group_report_statistics_url(id)
,您将获得完整的URL。有关详细说明,请参见手册的“Rails”部分中的路径和URL帮助器。
z0qdvdin2#
试试这个
或