# Can't call link\_to from within a model class

**URL:** <https://www.ruby-forum.com/t/cant-call-link-to-from-within-a-model-class/60308>\
**Category:** Rails\
**Created:** [May 11, 2006, 11:47pm UTC](https://www.ruby-forum.com/t/cant-call-link-to-from-within-a-model-class/60308 "2006-05-11T23:47:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![random52k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@random52k](https://www.ruby-forum.com/u/random52k)\
**Post date:** [May 11, 2006, 11:47pm UTC](https://www.ruby-forum.com/t/cant-call-link-to-from-within-a-model-class/60308/1 "2006-05-11T23:47:05Z")

</div>

can anyone tell me how to call link\_to from within a model class? I’m  
trying to do something like the following:

validates\_uniqueness\_of :email, :message =\> “address has already  
been taken. If you’ve forgotten your password, please click " +  
link\_to(‘here’, :action =\> ‘forgot\_password’, :controller =\> ‘site’) +  
" to reset it.”

although I get the following error when it attempts to validate a form:

Session contains objects whose class definition isn’t available.  
Remember to require the classes for all objects kept in the session.  
(Original exception: undefined method `link\_to’ for User:Class  
[NoMethodError])

I’ve also tried doing:

class User \< ActiveRecord::Base  
include ActionView::Helpers::UrlHelper

…

but calling link\_to produces the same error… I’ve also tried:

validates\_uniqueness\_of :email, :message =\> "address has already  
been taken. If you’ve forgotten your password, please click " +  
ActionView::Helpers::UrlHelper:link\_to(‘here’, :action =\>  
‘forgot\_password’, :controller =\> ‘site’) + " to reset it.

but that gives me:

Session contains objects whose class definition isn’t available.  
Remember to require the classes for all objects kept in the session.  
(Original exception: undefined method `link\_to’ for  
ActionView::Helpers::UrlHelper:Module [NoMethodError])

if anyone can help me, I’d really appreciate it… Thanks,

Mike

---

<div class="post-metadata">

**Author:** ![random52k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@random52k](https://www.ruby-forum.com/u/random52k)\
**Post date:** [May 11, 2006, 11:56pm UTC](https://www.ruby-forum.com/t/cant-call-link-to-from-within-a-model-class/60308/2 "2006-05-11T23:56:23Z")

</div>

On 5/11/2006, “Mike G.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> can anyone tell me how to call link\_to from within a model class? I’m  
> trying to do something like the following:

That would break the model/view/controller paradigm.

The model isn’t supposed to know anything about the view (or even how  
the data is rendered).

---

<div class="post-metadata">

**Author:** ![random52k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@random52k](https://www.ruby-forum.com/u/random52k)\
**Post date:** [May 12, 2006, 3:33pm UTC](https://www.ruby-forum.com/t/cant-call-link-to-from-within-a-model-class/60308/3 "2006-05-12T15:33:08Z")

</div>

On 5/11/06, Deirdre Saoirse M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 5/11/2006, “Mike G.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > can anyone tell me how to call link\_to from within a model class? I’m  
> > trying to do something like the following:
> 
> That would break the model/view/controller paradigm.
> 
> The model isn’t supposed to know anything about the view (or even how  
> the data is rendered).

true, but in my case, my choices are either using link\_to/url\_for or  
hardcoding a tag, and I wanted to avoid the latter. So yes,  
I realize this breaks the separation of the model from the view, but  
since the validation messages are provided by the model, I can’t think  
of how else I should do this… Thanks,

 

Mike

---

<div class="post-metadata">

**Author:** ![random52k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@random52k](https://www.ruby-forum.com/u/random52k)\
**Post date:** [May 12, 2006, 3:42pm UTC](https://www.ruby-forum.com/t/cant-call-link-to-from-within-a-model-class/60308/4 "2006-05-12T15:42:27Z")

</div>

Ultimately, the built in validation message methods really aren’t good  
enough for stuff like this. On a recent project in a similar situation,  
we used @object.errors.full\_messages to get an array containing all the  
error messages, and then produced custom output based on those errors.  
Its a lot less clean, but honestly, there’s not much way around it.
