Электронная почта не отправляется после отправки формы - PullRequest
0 голосов
/ 11 апреля 2020

Я пытаюсь завершить sh форму заявления, которая отправляется работодателю по электронной почте после нажатия кнопки отправки. У меня это работало несколько дней go, но я сделал так много небольших изменений в своем коде, что теперь форма не будет отправлена. Я могу отправить электронное письмо из консоли, хотя. Я просмотрел очень много разных постов и учебных пособий, но не могу понять.

Контроллер

    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 Ответ

1 голос
/ 11 апреля 2020

Ошибка в том, что в объекте @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.

...