Ruby on rails - Get NoMethodError (#<RailsConfig::Options address="smtp.sendgrid.net", port=587 的



我使用rails 4.1.6和设计版本3.4.1,每当用户做注册电子邮件将发送给用户。对于电子邮件,我已经使用Sendgrid和电子邮件发送延迟的工作,但我得到以下错误

INFO -- :   Parameters: {"utf8"=>"✓",  "authenticity_token"=>"4CwXT+J4dG1H9LqAlLpwnet4kGjLKXUVAXZl3n2FVv4=", "user"=>{"profile_attributes"=>{"first_name"=>"dd",  "last_name"=>"dd"}, "email"=>"test_email@yopmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}
[2015-05-21T11:22:12.638783 #22298]  INFO -- : Completed 500 Internal Server Error in 92ms
[2015-05-21T11:22:12.641223 #22298] FATAL -- : 
NoMethodError (undefined method `merge' for #<RailsConfig::Options address="smtp.sendgrid.net", port=587, domain="abc.com", user_name="abc", password="sdhs", authentication="plain", enable_starttls_auto=true>):
app/controllers/users/registrations_controller.rb:11:in `create'
注册控制器中的create方法是:
def create
  build_resource(sign_up_params)
  resource.add_role :User
  resource.save
  yield resource if block_given?
  if resource.persisted?
    if resource.active_for_authentication?
     set_flash_message :notice, :signed_up if is_flashing_format?
     sign_up(resource_name, resource)
     respond_with resource, location:   after_sign_up_path_for(resource)
   else
    set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
    expire_data_after_sign_in!
    respond_with resource, location: after_inactive_sign_up_path_for(resource)
  end
 else
  clean_up_passwords resource
  # set_minimum_password_length
  respond_with resource
 end
end

我得到以上错误,任何帮助是感激的。

问题是RailsConfig gem,你正试图将哈希值分配给smtp设置

类似于development.rbproduction.rb

config.action_mailer.smtp_settings = Settings.smtp_settings

Settings.smtp_settings的类是

Settings.smtp_settings.class
#=> RailsConfig::Options < OpenStruct

这个类没有merge方法

Settings.email_provider.merge({some_key: 2})
#=> NoMethodError: undefined method `merge' for #<Config::Options
#=>   address="smtp.sendgrid.net", port=587, domain="test.domain.com",
#=>   user_name="username", password="password", authentication="plain",
#=>   enable_starttls_auto=true>

所以你需要将值改为哈希值

Settings.email_provider.to_hash.class
#=> Hash < Object
Settings.email_provider.to_hash
#=> {
#=>                :address => "smtp.sendgrid.net",
#=>                   :port => 587,
#=>                 :domain => "test.domain.com",
#=>              :user_name => "username",
#=>               :password => "password",
#=>         :authentication => "plain",
#=>   :enable_starttls_auto => true
#=> }

或者更好的选择是替换

config.action_mailer.smtp_settings = Settings.smtp_settings
# to
config.action_mailer.smtp_settings = {
  address: Settings.smtp_settings.address,
  port: Settings.smtp_settings.port,
  domain: Settings.smtp_settings.domain,
  user_name: Settings.smtp_settings.user_name,
  password: Settings.smtp_settings.password,
  authentication: Settings.smtp_settings.authentication,
  enable_starttls_auto: Settings.smtp_settings.enable_starttls_auto
}

最新更新