Ruby on rails 为Gmail设置SMTP以使用RAILS创建“联系我们”页面时出错
我正在使用Rails设置Ruby on rails 为Gmail设置SMTP以使用RAILS创建“联系我们”页面时出错,ruby-on-rails,email,ruby-on-rails-4,smtp,contact-form,Ruby On Rails,Email,Ruby On Rails 4,Smtp,Contact Form,我正在使用Rails设置联系我们表单。我想把表格上的内容邮寄到我的个人帐户。我按照前面提到的说明进行操作,当我运行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 #<
联系我们
表单。我想把表格上的内容邮寄到我的个人帐户。我按照前面提到的说明进行操作,当我运行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'
最基本也是最简单的解决方案是:
#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
请粘贴development.rb文件(第37行附近),我刚刚编辑过它。我已经用我的开发更新了帖子。我想你在smtp设置上有问题。试试这个,我会用一个无表的模型来代替,并使用一个资源路由。你必须更改表单(资源的构建表单)和提交操作(更改参数名称)
#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