Url_for does not use default_url_options [: host]

I have a view for ActionMailer that includes several different links. I am running it on localhost: 3000 right now, so I installed this as such in a file called setup_mail.rb in app / initializers (as indicated here ):

 ActionMailer::Base.default_url_options[:host] = "localhost:3000" 

When I switch to using url_for in a view, this does not seem to pull this value. If I add :host => "localhost:3000" to each url_for tag, they work correctly. But they do not work without it.

I have another project_url tag that looks like this: a link to the specified project. This function, including the host value, has only project_url(@project) . Why work, not another?

From all that I read, setting default_url_options[:host] in the initializer should allow me to omit the value :host in the url_for tag. Obviously, it’s not the worst thing in the world to simply add this value, but it seems unnecessary, and that means that when I end up taking the project somewhere, I will have to go through and change this value everywhere. But worse, this is something that I do not understand. I am still involved when I go here, so I would like to know what I am doing wrong.

+4
source share
4 answers

The documentation is pretty clear for this

When you decide to set the default: host for your email programs, you need to use the option: only_path => false when using url_for. Since the url_for sub URL will generate relative default URLs if a: host is not explicitly specified, passing: only_path => false ensures that absolute URLs are created.

You can create your own helper instead of url_for to force :only_path be false

 def your_url_for(options = {}) options.reverse_merge! only_path: false url_for(options) end 

You can also use monkey patch rails to do this by default, but this is up to you :)

This will all be in addition to adding

 config.action_mailer.default_url_options = { host: "YOUR HOST" } 

up to config/application.rb or equivalent.

+3
source

It seems that parameter :only_path is false, which is the default. therefore, you need to provide [:host] either explicitly for each tag, or set the default parameters for url_for , which will apply to all tags. here's how to set the default host:

put this code in your application controller and it should work.

 helper_method :url_for def default_url_options(options) { host: 'localhost:3000' } end 

More info ... set url_for to default values

+2
source

Instead of changing the global default setting, which imho should not be changed after initialization, you can simply define the default_url_options method in your mailer, as you can do in the controller:

 class UserMailer < ActionMailer::Base def default_url_options { host: Tenant.current(true).host } end def confirm(user) @user = user mail(to: @user.email, subject: t(".subject_confirm")) end end 
+1
source

You set the default value in ActionMailer :: Base, but expect it to be reset by default for ActionController :: Base.

A <% = link_to%> inside your mailbox does not necessarily know anything that it is in the view of the mail program.

-2
source

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


All Articles