我无法弄清楚我哪里出错了。我的表单与大多数具有相同问题的人的写法不同,所以我无法正确配置它。我试图用附件提交我的表单,我收到以下错误:
这是我的控制器:
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
这是我的模特:
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
这是我的实际形式:
<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>
我的最终目标是能够附加各种格式的简历,例如pdf,word等,并将其发送到数据库。我正在使用paperclip gem for rails。
答案 0 :(得分:1)
尝试在表单标记中添加multipart/form-data
属性。使用具有文件上载控件的表单时需要此值
<form class="margin-clear" role="form" method="post" action="careers#create" enctype="multipart/form-data">