Forum: Ruby on Rails can't call link_to from within a model class

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0900e6a4828bd989f96427082c6c74ca?d=identicon&s=25 Mike Garey (random52k)
on 2006-05-11 23:47
(Received via mailing list)
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
5c0f4e71d9d0b71b8b60a2693df1571a?d=identicon&s=25 Deirdre Saoirse Moen (Guest)
on 2006-05-11 23:56
(Received via mailing list)
On 5/11/2006, "Mike Garey" <random52k@gmail.com> 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).
0900e6a4828bd989f96427082c6c74ca?d=identicon&s=25 Mike Garey (random52k)
on 2006-05-12 15:33
(Received via mailing list)
On 5/11/06, Deirdre Saoirse Moen <deirdre@deirdre.net> wrote:
> On 5/11/2006, "Mike Garey" <random52k@gmail.com> 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 <a href> 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
Fb23bc8cd4030c526b0689276b34c8bd?d=identicon&s=25 Bryan Duxbury (bryanduxbury)
on 2006-05-12 15:42
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.
This topic is locked and can not be replied to.