提交表单后未发送电子邮件

时间:2020-04-11 00:14:17

标签: ruby-on-rails email mail-form

我正在尝试填写一份申请表格,一旦按下“提交”按钮,该表格就会通过电子邮件发送给雇主。几天前我可以使用它,但是我对代码进行了许多小的更改,现在该表单无法发送。我可以从控制台内部发送电子邮件。我看过很多不同的文章和教程,但我不知道。

控制器

    def new
       @form = Form.new
    end 

    def create
        @form = Form.new(params)
      if @form.deliver
        redirect_to careers_path, :notice => 'Thank you for applying.'
      else
        redirect_to new_form_path, :notice => 'Message could not be sent. Please try again.'
      end
   end

模型

class Form < MailForm::Base
  attribute :name,      :validate => true
  attribute :email,     :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
  attribute :salary, :validate => true
  attribute :location, :validate => true
  attribute :message, :validate => true

  # Declare the e-mail headers. It accepts anything the mail method
  # in ActionMailer accepts.
  def headers
    {
      :subject => "Applicant",
      :to => "123@email.com",
      :from => %("#{name}" <#{email}>)
    }
  end
end

查看

<%= simple_form_for new_form_path, method: :post do |f| %>
      <div>
          <%= f.label :name %><br/>
          <%= f.text_field  :name, required: true, class: "contact-form-text-area" %></br>
          </br>
          <%= f.label :email %><br/>
          <%= f.text_field :email, required: true, class: "contact-form-text-area" %></br>
          </br>
          <%= f.label :location %><br/>
          <%= f.text_field :location, required: true, class: "contact-form-text-area" %></br>
          </br>
          <%= f.label :salary %><br/>
          <%= f.text_field :salary, required: true, class: "contact-form-text-area" %></br>
          </br>
        <%= f.label :message %><br/>
        <%= f.text_area :message, rows: 8, cols: 40, required: true, class: "contact-form-text-area",
                          placeholder: "Inlcude extra details"%></br>

        <br/>
        <div align="center">
          <%= f.submit 'Submit', class: 'btn btn-primary' %>
        </div>
      </div>
    <% end %>

development.rb

config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors= true
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address:              'smtp.gmail.com',
    port:                 587,
    domain:               'gmail.com',
    user_name:            ENV["GMAIL_EMAIL"],
    password:             ENV["GMAIL_PASSWORD"],
    authentication:       'plain',
    enable_starttls_auto: true  }

路线

resources :forms, only: [:new, :create]
    post '/forms/new', to: 'forms#create'

登录

Started POST "/forms/new" for ::1 at 2020-04-10 18:46:10 -0700
Processing by FormsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"hwQ5eKNZptSK0w8E4C+UdvS+7TuUg5esYXyp8FMt/008s+RAiRrt/0oXpKVwGbJ5JbOpjtqbAqxTO/aoDWXZMA==", "/forms/new"=>{"name"=>"person", "email"=>"123@email.com", "location"=>"somewhere", "salary"=>"$$$", "message"=>"testing"}, "commit"=>"Submit"}
Redirected to http://localhost:3000/forms/new
Name can't be blank
Email can't be blank
Salary can't be blank
Location can't be blank
Message can't be blank
Completed 302 Found in 5ms (ActiveRecord: 0.0ms)


Started GET "/forms/new" for ::1 at 2020-04-10 18:46:10 -0700
Processing by FormsController#new as HTML
  Rendering forms/new.html.erb within layouts/application
  Rendered forms/new.html.erb within layouts/application (4.0ms)
Completed 200 OK in 197ms (Views: 194.7ms | ActiveRecord: 0.0ms)

1 个答案:

答案 0 :(得分:1)

错误是@form对象中未设置任何内容。

根据日志,参数正在传入...

Parameters: {"utf8"=>"✓", "authenticity_token"=>"token", 
"/forms/new"=>{"name"=>"person", "email"=>"123@email.com",
"location"=>"somewhere", "salary"=>"$$$", "message"=>"testing"}, 
"commit"=>"Submit"}

,但未设置为对象。所以,我的赌注是这两行

@form = Form.new(params[:form])
@form.request = request

应替换为

@form = Form.new(params)

那应该设置@form对象的所有属性。