relative_url_root in ActionMailer - ruby-on-rails

What's the equivalent of relative_url_root in ActionMailer?
I've tried setting it up in default_url_options, but it appends the parameter in the query string.
Adding :skip_relative_url_root = false doesn't work either.
Using :host = 'somehost.com/subdir' does work, but is that appropriate?
Thanks!

A cleaner way to do it :
config.action_mailer.default_url_options = {
:host => "somehost.com",
:only_path => false,
:script_name => "/subdir"
}
Source

Use script_name option into default_url_options of ActionMailer::Base. Based on this article.

Related

How to tag every log call in rails with request id (lograge)

I'm using lograge with rails and I have configured my logs using JSON format. I would like every time I call logger.info,logger.warn, etc. to include the request uuid. The way rails handles this with tagged logging falls short of what I would like because it does not seem able to merge the request uuid with the remainder of the JSON payload, instead prepending it on the line in non-JSON format.
For instance, if I call logger.info(client: :ig) I would expect the following log output:
{"request_id": <request uuid>, "client": "ig"}
But instead rails will prepend the request uuid (when configured via config.log_tags = [:uuid]) like so:
[<request uuid>] {"client": "ig"}
Does anyone know if there is a way to get the tagging behavior to merge with the JSON payload instead of prepending it on the same line? I'd like to configure our logs to forward to Splunk using just a simple JSON formatter and not deal with this prepending format.
Also, I have configured lograge to include request_id set to the request uuid in a lambda passed to custom_options in config/application.rb. This works but only when rails logs the request. If I explicitly call one of the logging methods anywhere else, the request_id is not included.
# application.rb
config.lograge.enabled = true
config.lograge.formatter = Lograge::Formatters::Json.new
config.lograge.custom_options = lambda do |e|
{
params: e.payload[:params].except("controller", "action", "utf8"),
request_id: e.payload[:request_id] # added this in `append_info_to_payload` in ApplicationController
}
end
Then in config/environments/production.rb
config.log_tags = [ -> (req) { { request_id: req.env["action_dispatch.request_id"] } } ]
Any help is appreciated, thanks.
The problem is that the payload doesn't have the request_id.
As you can see in:
./actionpack-3.2.11/lib/action_controller/metal/instrumentation.rb:18-25
raw_payload = {
:controller => self.class.name,
:action => self.action_name,
:params => request.filtered_parameters,
:format => request.format.try(:ref),
:method => request.method,
:path => (request.fullpath rescue "unknown")
}
I override this method (copy ./actionpack-3.2.11/lib/action_controller/metal/instrumentation.rb in config/initializer.rb) and add your param.
raw_payload = {
:controller => self.class.name,
:action => self.action_name,
:params => request.filtered_parameters,
:format => request.format.try(:ref),
:method => request.method,
:path => (request.fullpath rescue "unknown"),
:request_id => env["action_dispatch.request_id"]
}
Maybe there are a better way for override instrumentation, but it is enough.
Is easier to override initializer with class_eval as you can see in:
Access to Rails request inside ActiveSupport::LogSubscriber subclass

Rails - How to access the current environment's host URL?

in my rails app for my environments, i have the following:
config.action_mailer.default_url_options = { :host => '0.0.0.0:3000' }
How can I access that param in a user_mailer?
I want to get what host equals.
Thanks
ActionMailer::Base.default_url_options[:host]

Rails: generate a full URL in an ActionMailer view

I'm using ActionMailer to send a sign up confirmation email. The email needs to contain a link back to the site to verify the user, but I can't persuade Rails to generate a full URL (including the domain etc).
I'm using:
<%= url_for :controller => 'login', :action => 'verify', :guid => #user.new_user.guid, :only_path => false, :host => 'http://plantality.com' %>
in my view
Part b:
In development mode Rails gripes if I don't specify the host explicilty in the link above. But I don't want to do this in production. Any solutions?
To solve the problem to pass a host for generating URLs in ActionMailer, check out this plugin and the reason why I wrote it.
To solve the first issue, use named routes when applicable. Instead of
<%= url_for :controller => 'login', :action => 'verify', :guid => #user.new_user.guid, :only_path => false, :host => 'http://plantality.com' %>
assuming the route is called login, use
<%= login_url(:guid => #user.new_user.guid) %>
Note, I'm using login_url, not login_path.
I'm not sure if it is what you want but in config/environments/development.rb you can specify default options for mailer urls
config.action_mailer.default_url_options = {
:host => "your.host.org",
:port => 3000
}
you can do the same in config/environments/production.rb
I don't know why the previous solutions seem so complicated, but since I'm here why not give my 2 cents...
Go to /config/environments and add:
config.absolute_site_url = 'your site url'
for the respective environment (ie. in development.rb, test.rb, or production.rb). Restart web server.
This allows you to call Rails.application.config.absolute_site_url to get the desired URL. No need for plugins or weird cheat, just store the site url as an application wide variable.
I think its not 100% correct way but this can also be a solution :
See the Using asset hosts section in the documentation. You need to specify an asset_host. You can also construct it dynamically from the request chaining "#{request.protocol}#{request.host_with_port}/login/?guid=#{#user.new_user.guid}"
To generate url, try this
Rails.application.routes.url_helpers.user_url(User.first.id, host: 'yourhost.io')
this will generate url like this:
http://yourhost.io/users/1
As well you can pass some params
expires = Time.now + 2.days
params = {expires: expires}
u = User.first.id
Rails.application.routes.url_helpers.user_url(u, params, host: 'host.com')
will generate:
http://yourhost.io/users/1.expires=2018-08-12+15%253A52%253A15+%252B0300
so you can werifi in action if link is not expired

localhost on rails

I am sending an email that contains a link to my website. I want to be able to test it locally and be able to move the scripts around to different hosts easily.
In my email right now I use the following:
<%= url_for(:host => 'localhost:3000', :controller => "user_activations", :action => "show", :id=>#id, :confirm=>#passcode) %>
This works for when testing locally but will obviously fail for production. Is there an easy way to have rails (or ruby) detect what the current host is? I'm thinking something like $_SERVER of php.
I realize I can use some logic using my environment variable but I would like to avoid this.
Thanks
I define a constant 'HOST' in my environment.rb that sets my host. Alternatively you can use request.host or request.domain.
in environments/development.rb
config.action_mailer.default_url_options = { :host => "localhost", :port => 3000 }
in environments/production.rb
config.action_mailer.default_url_options = { :host => "www.xyu.at" }
and use tests with rspec-email :)

How do I configure the hostname for Rails ActionMailer?

I'm working on a fairly traditional forgot password email - I want to email the user a password change token embedded in a link that they can click on in order to change their password. I'm emailing via the traditional ActionMailer.
If I use a normal link_to tag
<%= link_to "click here", :controller => foo, :action => 'bar', :token => token %>
I get a relative link - rather useless from an email.
If I add in
:only_path => false, then it errors saying I need to set default_url_options[:host]. The ActionController docs imply that you do that by overriding the #default_url_options methods in your controller. Surely there's a configuration option to tell Rails what it's hostname is without adding my own config file, parsing it, etc?
default_url_options is available from config.action_mailer and should be set in your environment's configuration file.
For example, in config/environments/production.rb:
config.action_mailer.default_url_options = {
:host => 'www.yourdomain.com'
}
For local testing, modify config/environments/development.rb:
config.action_mailer.default_url_options = {
:host => '127.0.0.1',
:port => 3000
}
Then, assuming you have a named route called forgot_password_login, you can generate the login link URL in your mailer using something like this:
forgot_password_login_url(:token => 'a7s8q15sk2...')
You probably want to set :protocol => 'https' as well, btw.
config.action_mailer.default_url_options = {
:host => "portal.example.com",
:protocol => 'https'
}
There is another alternative, as described in http://pivotallabs.com/how-i-leaned-to-stop-hating-and-love-action-mailer/
This solution has the advantage that it doesn't require any configuration (so less of a hassle), and works fine as long as you send emails from within controllers.
But if you plan on sending email without going through a controller (e.g. from command line or in response to another email), you need the static configuration.
Setting default_url_options directly is deprecated in Rails 3.1. Use url_for instead.
Add parameter :protocol to override default value (http), :protocol => 'https://'. This will create url starting with "https://..." instead of default "http://"
Interestingly, I had the same issue as you did, but in unit tests (while following Michael Hartl's railstutorial). I had this line in my test.rb file, but that didn't help:
config.action_mailer.default_url_options = { host: 'example.com', protocol: 'http' }
I've also added another line like this to test.rb, and surprisingly this solved the issue
default_url_options = { host: 'example.com', protocol: 'http' }
Setting default_url_options directly is deprecated in Rails 3.1
Use the url_for helper to create it:
<%= link_to "click here", url_for(:controller => foo, :action => 'bar', :token => token, :host => 'www.yourdomain.com') %>
Can you just do
<%="click here", :controller => foo, :action => 'bar', :token => token, :host=>request.host -%>

Resources