Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails get url_for working inside model

Title says it all. It's pretty easy from controllers - just using "view_context". But it doesn't work with models.

E.g ApplicationController.new.view_context.url_for yields in: "undefined method 'host' for nil:NilClass"

ActionView::Base.new(Rails.configuration.paths["app/views"].first).url_for doesn't work as well.

Can this be done without so much pain ?

P.S I need it for my Prawn gem! I wanted to generate the PDF from the model. And apart from generating the links inside of it - it is very successful!

Hope for your help! Thanks in advance!

like image 541
Dmitri Avatar asked Oct 26 '25 08:10

Dmitri


1 Answers

Rails 5

include Rails.application.routes.url_helpers

url_for(myModelObject)
like image 85
Confused Vorlon Avatar answered Oct 28 '25 23:10

Confused Vorlon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!