How do I get the path to a resource in the model? I need to store it in the database and would like the same url as you get from resourcename_path(resource)
In Rails 3 and Rails 4 you can use:
Rails.application.routes.url_helpers
e.g.
Rails.application.routes.url_helpers.posts_path
In Rails 3, you can include the url helpers in models (though it normally is not the best way of dealing with things):
class MyClass < ActiveRecord::Base
include Rails.application.routes.url_helpers
end
In Rails 2, I think it's include ActionController::UrlWriter
, but I can't remember. Google is your friend.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With