开发者

Rails, get resource path in model [duplicate]

This question already has answers here: Can Rails Routing Helpers (i.e. mymodel_path(model)) be Used in Models? (7 answers) Closed 8 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜