获取NoMethodError(未定义方法`merge&#39; for#<railsconfig :: options address =“”smtp.sendgrid.net“,”port =“587”

时间:2015-05-21 13:19:26

标签: ruby-on-rails devise sendgrid

=“”

我正在使用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

我遇到上述错误,感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

问题是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
}