Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails, get resource path in model [duplicate]

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)

like image 867
jonepatr Avatar asked Mar 21 '11 16:03

jonepatr


2 Answers

In Rails 3 and Rails 4 you can use:

Rails.application.routes.url_helpers

e.g.

Rails.application.routes.url_helpers.posts_path
like image 139
hrdwdmrbl Avatar answered Nov 19 '22 02:11

hrdwdmrbl


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.

like image 45
idlefingers Avatar answered Nov 19 '22 02:11

idlefingers