views:

17

answers:

1

Hi,

I'd like to give a link to the contact us page on failure of a validation. I've tried this to no avail:

validates_acceptance_of :not_an_agency, :on => :create,
:message => "must be confirmed. If you are an agency please #{link_to "Contact Us, contact_path}"

Anyone know how to get past this one?

Jack

A: 

With Rails 3

you need include ActionView::Helpers::UrlHelper in your Model and define the message like a lambda to be interpret when needed

class XXX < AR
  extend ActionView::Helpers::UrlHelper

  validates_acceptance_of :not_an_agency, :on => :create,
                          :message => lambda {"must be confirmed. If you are an agency please #{link_to "Contact Us", contact_path}"}

end

With Rails 2

it's the same but you need define the :host each time.

class XXX < AR
  extend ActionView::Helpers::UrlHelper

  validates_acceptance_of :not_an_agency, :on => :create,
                          :message => lambda {"must be confirmed. If you are an agency please #{link_to "Contact Us", contact_path(:host => 'http://example.org')}"}

end
shingara
Thanks for the quick reply. I tried your Rails 3 solution but am still getting the same error - undefined local variable or method `contact_path' for #<Class:0x35f95e4>
Jack Kinsella
BTW there is a small typo- you didn't close the string after "Contact Us"
Jack Kinsella
I update my answer with typo fix. So it's more extend than include :)
shingara