【问题标题】:Error setting up SMTP for Gmail for making a Contact Us Page using RAILS为 Gmail 设置 SMTP 以使用 RAILS 创建联系我们页面时出错
【发布时间】:2014-06-24 02:34:36
【问题描述】:

我正在关注 this tutorial 以使用 Rails 设置 Contact Us 表单。我希望将表格上的内容邮寄到我的个人帐户。我按照上述说明进行操作,当我运行 foreman start 时,出现以下错误。

/home/adhithya/.rvm/gems/ruby-2.1.1@railstutorial_rails_4_0/gems/railties-4.0.4/lib/rails/railtie/configurable.rb:30:in `method_missing': undefined method `“smtp' for #<PersonalWebsite::Application:0x9b6e4c4> (NoMethodError)
from /home/adhithya/Desktop/RailsDev/rocky-oasis-9687/config/environments/development.rb:37:in `block in <top (required)>'
from /home/adhithya/.rvm/gems/ruby-2.1.1@railstutorial_rails_4_0/gems/railties-4.0.4/lib/rails/railtie/configurable.rb:24:in `class_eval'

我还没有安装 Devise,因为我没有登录模型,所以我不需要在我的网站上进行任何身份验证。

我想知道如何让它工作。此外,关于使用 Rails 为单页网站实现简单的联系我们表单的最佳方式的建议也会有所帮助。

我的 development.rb 是这样的

config.action_mailer.smtp_settings = {
  address: “smtp.gmail.com”,
  port: 587,
  domain: ENV["GMAIL_DOMAIN"],
  authentication: “plain”,
  enable_starttls_auto: true,
  user_name: ENV["GMAIL_USERNAME"],
  password: ENV["GMAIL_PASSWORD"]
  }

【问题讨论】:

  • 请粘贴 development.rb 文件(大约第 37 行)
  • 我刚刚编辑了它。我已经用我的 development.rb 更新了帖子
  • 我认为您的 smtp 设置有问题。试试这个stackoverflow.com/questions/23300150/…

标签: ruby-on-rails email ruby-on-rails-4 smtp contact-form


【解决方案1】:

基本和最简单的解决方案是:

#config/routes.rb
match "contact" => "welcome#contact", :as=> "contact", :via=>[:get,:post]


#controllers/welcome_controller.rb
def contact
   if(params[:name] && params[:message] && params[:email])
    NotificationsMailer.send_contact(params[:name] ,params[:email],params[:message]).deliver
    redirect_to root_path, notice: t('notice.welcome.contact')
  else
    render 'welcome/contact'
  end
end

#views/welcome/contact.html.haml
%form{:method => :post}
  %h2 Contact form
  %input{:type=>"hidden", :name=>"authenticity_token", :value=>form_authenticity_token.to_s}
  %div
    %label.control-label{:for=>"name"}
      Name
    %input{:name=>"name", :type=>"text", :required=>true, :maxlength => 50}
  %div
    %label{:for=>"email"}
      Email
    %input{:name=>"email", :type=>"email", :required=>true}

  %div
    %label.control-label{:for=>"message"}
      Message
    %textarea{:name=>"message", :id=>"message", :rows=>'5', :required=>true}

  %div.form-actions
    %input.button{:type=>"submit", :value=>"Send"}

#mailers/notifications_mailer.rb
def send_contact(name,email,message)
  @name=name
  @email = email
  @message = message
  mail(:subject=>"Message form contact form")
end

【讨论】:

  • 我会使用无表模型和个人资源路由
  • 您必须更改表单(为资源构建表单)和提交操作(更改参数名称)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-18
  • 1970-01-01
  • 1970-01-01
  • 2017-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多