Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9136315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:56:12+00:00 2026-06-17T08:56:12+00:00

I’m trying to access request.host (well, ideally host_with_port ) from a Mailer in Rails.

  • 0

I’m trying to access request.host (well, ideally host_with_port) from a Mailer in Rails. The actually call to request.host is in a Helper:

#/app/helpers/confirmations_helper
module ConfirmationsHelper
  def email_confirm_url(token)
    "http://#{request.host_with_port}/confirm/#{token}" # failure: undefined method
  end
end

#/app/mailers/user_mailer
class UserMailer < ActionMailer::Base
  default from: "email@domain.com"

  add_template_helper(ConfirmationsHelper) #get access to helpers/confirmations_helper.rb

  def email_confirmation(user)
    @user = user
    @url = "http://www.domain.com/"
    mail(to: user.email, subject: "Email Confirmation")
  end
end

#config/environments/development.rb
...
config.action_mailer.default_url_options = { :host => "localhost:3000" }

Error I’m getting is:

 ActionView::Template::Error:
   undefined method `host' for nil:NilClass
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:56:13+00:00Added an answer on June 17, 2026 at 8:56 am
    ActionView::Template::Error:
      undefined method `host' for nil:NilClass
    

    This is telling you that request is nil. This is because outside of the scope of your controller (ie. in a class extending ActionMailer::Base) request doesn’t exist.

    You need to pass the request object or just the part you need (request.host_with_port) to the mailer like you do other data like user in your email_confirmation.


    So you have a create method with something like this

    def create
      @user = User.new
    
      @user.assign_attributes(params[:user])
      @user.save
    
      @user.send_email_confirmation
    end
    

    Inside your User model you have a send_email_confirmation method like this

    class User < ActiveRecord::Base
      def send_email_confirmation
        UserMailer.email_confirmation(self).deliver
      end
    

    Your mailer’s email_confirmation looks like

    def email_confirmation(user)
      @user = user
      @url = "http://www.domain.com/"
      mail(to: user.email, subject: "Email Confirmation")
    end
    

    Making the request to the mailer from your model is not the best idea; you should keep a cleaner separation of concerns. This is part of your problem and why you are finding unwanted complexity when trying to pass something like request from your controller action into the mailer template.

    What I might suggest is creating a worker class. Here I explain how to setup classes in lib/ – the same concept can be applied to something like a lib/your_app/workers/user.rb.

    You could have the following in this class

    module YourApp
      module Workers
        module User
          extend self
    
          def create!(params, options{})
            options.reverse_merge! host: ""
    
            user = User.new
    
            user.assign_attributes(params)
            user.save
    
            UserMailer.email_confirmation(user, host).deliver
    
            user
          end
        end
      end
    end
    

    Your controller action could then simply be

    def create
      @user = ::YourApp::Worker::User.create!(params[:user], host: request.host_with_port)
    end
    

    Your mailer method can now look like

    def email_confirmation(user, host)
      @user = user
      token = "" # define token somehow
      @url = "#{host}/confirm/#{token}"
      mail(to: user.email, subject: "Email Confirmation")
    end
    

    Finally, you can remove send_email_confirmation from your model as well as the email_confirm_url method from your helper since they’re no longer used. Two things to note

    • my example above doesn’t include anything in the way of validations/error-checks
    • my example makes an assumtion about where token is being defined and used

    As you can see, by introducing this ‘worker’ class, there is a clean separation of functionality without duplication.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
We're building an app, our first using Rails 3, and we're having to build
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a view passing on information from a database: def serve_article(request, id): served_article
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.