I'm trying to finish an application form that gets emailed to an employer once the submit button is hit. I had it working a few days ago, but I made so many little changes to my code that now the form won't send. I can send an email from inside the console though. I've looked at so many different posts and tutorials but I can't figure it out.
Controller
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
Model
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 => "[email protected]",
:from => %("#{name}" <#{email}>)
}
end
end
View
<%= 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 }
routes
resources :forms, only: [:new, :create]
post '/forms/new', to: 'forms#create'
Log
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"=>"[email protected]", "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)
The error is that nothing in the @form object is set.
According to the log, the parameters are being passed in...
but not being set into the object. So, my bet is these two lines
Should be replaced by
That should set all the properties on the @form object.