Can't call link_to from within a model class


#1

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


#2

On 5/11/2006, “Mike G.” 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).


#3

On 5/11/06, Deirdre Saoirse M. removed_email_address@domain.invalid wrote:

On 5/11/2006, “Mike G.” 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


#4

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.