Is it possible to get the user_path in the User model? I'm trying to implement the following method:
def link_path
link_to self.name, user_path(self.id_
end
But i'm getting that user_path is undefined. Any ideas?
Is it possible to get the user_path in the User model? I'm trying to implement the following method:
def link_path
link_to self.name, user_path(self.id_
end
But i'm getting that user_path is undefined. Any ideas?
How about ...
def link_path
Rails.application.routes.url_helpers.user_path(self)
end
I've done this sort of thing in APIs - where I want the url to the site to be in the json of the API response.
That kind of method usually sits in a helper:
UsersHelper.rb
def user_link_path(user)
link_to user.name, user_path(user)
end
View:
<%= user_link_path(user) %>
You can use url helpers in your models if you really want to, but I don't see any reason to for what you're doing, so may as well do it the rails way.