Rails, get resource path in model [duplicate]
How do I get t开发者_运维技巧he 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.
精彩评论