0

I create a mail which works fine as long as I do not add a link inside the body. I am looking to have an email that is send to a recipient once a message is directed to him on the platform.

<%= link_to "See message", @message %> does not seem to work. Is there a specific approach for the problem ? All what I saw so far is not very clear.

The error message is :

ActionController::UrlGenerationError in Rails::Mailers#preview

Message-mailer.rb file :

class MessageMailer < ApplicationMailer

  def new_message(message)
    @message = message

    @title = message.title
    @from_first_name = User.find_by_id(message.user_id).first_name
    @from_last_name = User.find_by_id(message.user_id).last_name


    mail to: @message.user.email,
         subject: "New Message: #{@message.title}"

  end
end

1 Answers1

0

Mailer is not inside the response stack, you need to specify the host or make it default from env.

Similar Answer Link and Action Mailer Link

<p>Hi <%= "#{@email}"%>,</p>
<p>
    Message Link: <%=  link_to "See Message", 
                        message_url(id: @message.id, host: "http://localhost:3000")%>    
</p>

# development.rb - for default
config.action_mailer.default_url_options = { :host => "localhost" }
7urkm3n
  • 6,054
  • 4
  • 29
  • 46
  • Hi, i just tried, and I have an error again, `missing required keys: [:id], possible unmatched constraints: [:locale]` –  May 31 '18 at 08:22
  • share yr message_mailer.rb file – 7urkm3n May 31 '18 at 08:28
  • Hello, I just added the file in the original message –  May 31 '18 at 08:33
  • @Etienne just updated my answer `id: @message.id` try like this, forgot using `url` instead `path`. – 7urkm3n May 31 '18 at 08:47
  • Hey, thanks a lot, i now have the missing Locale key : `ActionView::Template::Error (No route matches {:action=>"show", :controller=>"messages", :id=>83}, missing required keys: [:locale])` Much more difficult than php :( –  May 31 '18 at 09:55
  • @Etienne if using locale, try adding `locale: :us` or nil. – 7urkm3n May 31 '18 at 14:43