Getting "path" info from the model
**Update: i actually have a pretty good reason. I have a notify class that sends out links to users. So i need to have the URL information in there, simply for the very same routing reasons as the front end ;)
how do i get "path" routes when i am in the model?
users_pat开发者_StackOverflow社区h(xxx)
app.users_path(xxx)
Both of this fails
Thats a bad idea, you shouldn't have to requires routes information inside a model.
If you do, there is definitely something wrong, try and use helpers instead of stuff inside the model.
Or you could provide some more details about the problem and We could provide a definite answer.
Update - Assuming, you are sending out emails to your users, you can directly use url helpers in your mailer views ( instead of doing in the model ) . Just remember to use user_url(xxx) instead of user_path(xxx). For this you may have also have to set the action_mailer.default_url_options[:host].
While I do agree that in most cases, this should be avoided and there's often a better way of doing it, it's quite short sighted to say that it's never needed. In fact, there's a number of circumstances where I would say this is perfectly acceptable (for example, generating csv reports which need to have a link to the object's page on the site).
In any case, Rails makes this quite easy to do. Just make sure you've thought through whether it is appropriate to do first! Just include Rails url helpers in your class...
include Rails.application.routes.url_helpers
精彩评论