Rails::MailersController#preview 中的 ArgumentError

Posted

技术标签:

【中文标题】Rails::MailersController#preview 中的 ArgumentError【英文标题】:ArgumentError in Rails::MailersController#preview 【发布时间】:2016-03-02 10:38:41 【问题描述】:

我正在编写 Michael Hartl 编写的 Ruby on Rails 教程中的 10 章,我正面临有关帐户激活的问题

Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true

user_mailer.rb

class UserMailer < ApplicationMailer

  def account_activation(user)
    @user = user
    mail to: user.email, subject: "Account activation"
  end

  def password_reset
    @greeting = "Hi"
    mail to: user.email
  end
end

account_activation.html.erb

<h1>Sample App</h1>

<p>Hi <%= @user.name %>,</p>

<p>
Welcome to the Sample App! Click on the link below to activate your account:
</p>

<%= link_to "Activate", edit_account_activation_url(@user.activation_token,
                                                    email: @user.email) %>

app/views/user_mailer/account_activation.html.erb:9:in `_app_views_user_mailer_account_activation_html_erb__2222007018826719766_70083837842380'
app/mailers/user_mailer.rb:5:in `account_activation'

【问题讨论】:

***.com/a/799383/2057388 我尝试更改config.action_mailer.default_url_options的格式,没有成功。 【参考方案1】:

更新

default_url_options 添加到config/environments/development.rb

config.action_mailer.default_url_options =  :host => '127.0.0.1', :port => 3000 

config.action_mailer.default_url_options =  :host => 'localhost', :port => 3000 

【讨论】:

Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true 这是我在development.rb中设置的default_url_options config.action_mailer.default_url_options = :host =&gt; '127.0.0.1', :port =&gt; 3000 我停止了服务器,然后重新启动它,它现在可以工作了。谢谢

以上是关于Rails::MailersController#preview 中的 ArgumentError的主要内容,如果未能解决你的问题,请参考以下文章