How to create a link to rails with an AR verification message

I would like to provide a link to a page with a message about the denial of validation. I 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}"

Does anyone know how to get past this?

Jack

+3
source share
3 answers

Like Rails 3.1, you can also:

view_context.link_to "Contact Us", contact_path

In addition, to be strict, the original example code for the poster doesn’t have a close quote from the Contact Us section.

+1
source

With Rails 3

you need to include ActionView :: Helpers :: UrlHelper in your model and define a message, for example, a lambda that needs to be interpreted if necessary

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

this is the same, but you need to define: host every 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
0
source

Shingara "link_to", - "contact_path". :

validates_acceptance_of :not_an_agency, :on => :create,
                      :message => lambda {|e,f| "must be confirmed. If you are an agency please <a href=\"#{Rails.application.routes.url_helpers.contact_path}\">Contact Us</a>".html_safe}

<a href.../> , include ActionView::Helpers::UrlHelper. .

Also pay attention to .html_safe. This is actually useless because Rails loses it when it adds a field name and you have to make it safe on the view side again. But I put it anyway in the hope that Rails will eventually fix this part of the violation.

And I have no idea that | e, f | for. I had to insert them in order to fix the wrong argument mismatch error.

0
source

Source: https://habr.com/ru/post/1768400/


All Articles