I cannot figure out where I went wrong. My form is written different then most people with the same issue so I have not been able to configure it properly. I tried to submit my form with an attachment and I got the following error:
Here is my controller:
class CareersController < ApplicationController
def new
@career = Career.new
end
def show
@career = Career.find(params[:id])
end
def create
# fail
@career = Career.create(career_params)
if @career.save
CareerMailer.career_inquiry(@career).deliver
redirect_back(fallback_location: root_path)
else
flash[:error] = @career.errors.full_messages
redirect_back(fallback_location: root_path)
end
end
private
def career_params
params.require(:career).permit(:name, :phone, :subject, :email, :message, :document)
end
end
Here is my model:
class Career < ApplicationRecord
has_attached_file :document
validates_attachment_size :document, :less_than => 25.megabytes
validates_attachment_presence :document
validates_attachment_content_type :document, :content_type => ["application/pdf","application/vnd.ms-excel",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"application/msword",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"text/plain"]
email_regex = /\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
validates :name, :presence => true,
:length => { :maximum => 50 }
validates :subject, :presence => true,
:length => { :maximum => 50 }
validates :phone, :presence => true,
:length => { :maximum => 50 }
validates :email, :presence => true,
:format => {:with => email_regex }
validates :message, :presence => true,
:length => { :maximum => 5000 }
end
Here is my actual form:
<form class="margin-clear" role="form" method="post" action="careers#create" >
<input type="hidden" name="authenticity_token" value="<%=form_authenticity_token%>">
<div class="form-group has-feedback">
<label for="name">Name*</label>
<input type="text" class="form-control" id="name" name="career[name]" placeholder="">
<i class="fa fa-user form-control-feedback"></i>
</div>
<div class="form-group has-feedback">
<label for="email">Email*</label>
<input type="email" class="form-control" id="email" name="career[email]" placeholder="">
<i class="fa fa-envelope form-control-feedback"></i>
</div>
<div class="form-group has-feedback">
<label for="phone">Phone Number*</label>
<input type="phone" class="form-control" id="phone" name="career[phone]" placeholder="">
<i class="fa fa-phone form-control-feedback"></i>
</div>
<div class="form-group has-feedback">
<label for="subject">Subject*</label>
<input type="text" class="form-control" id="subject" name="career[subject]" placeholder="">
<i class="fa fa-navicon form-control-feedback"></i>
</div>
<div class="form-group has-feedback">
<label for="subject">Upload Resume*</label>
<input type="file" class="form-control" id="file" name="career[document]" placeholder="">
<i class="fa fa-file form-control-feedback"></i>
</div>
<div class="form-group has-feedback">
<label for="message">Message*</label>
<textarea class="form-control" rows="6" id="message" name="career[message]" placeholder=""></textarea>
<i class="fa fa-pencil form-control-feedback"></i>
</div>
<!-- <div class="g-recaptcha" data-sitekey="your_site_key"></div> -->
<input type="submit" value="Submit" class="submit-button btn btn-default">
</form>
My ultimate goal is to be able to attach a resume in a variety of formats such as pdf, word, et cetera and have it sent to the database. I am using paperclip gem for rails.