简体   繁体   中英

Rails get controller action url

I have an action, and I've defined it in the routes.rb

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

Then in the method in the model, I want to return the full URL.

Thus, I wrote this method

root_url + group_report_statistics_path(id)

The result is "http://localhost:3000//statistics/group_report/DHo6qzUzYXw"

I thought it was a dirty way, and I need to handle the double slash as well.

Does Rails provide some useful method which I can get the full URL of the action directly?

Use group_report_statistics_url(id)

_path helpers provide a site-root-relative path. You should probably use this most of the time.

_url helpers provide an absolute path, including protocol and server name. I've found that I mainly use these in emails when creating links to the app on the server. They should mainly be used when providing links for external use.

Rails provides path as well as url helpers for all the routes.

So if you use group_report_statistics_url(id) , you will get the full URL.

try this

File.join(root_url + group_report_statistics_path(id))

or

 group_report_statistics_url(id)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM