Rails 5 Paperclip::AdapterRegistry::NoHandlerError
Rails 5 Paperclip::AdapterRegistry::NoHandlerError
我想不通哪里出错了。我的表单与大多数有相同问题的人的编写方式不同,因此我无法正确配置它。我试图提交带有附件的表单,但出现以下错误:
这是我的控制器:
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 等,并将其发送到数据库。我正在为 rails 使用回形针 gem。
尝试在您的表单标签中添加 multipart/form-data
属性。当您使用具有文件上传控件的表单时需要此值
<form class="margin-clear" role="form" method="post" action="careers#create" enctype="multipart/form-data">
我想不通哪里出错了。我的表单与大多数有相同问题的人的编写方式不同,因此我无法正确配置它。我试图提交带有附件的表单,但出现以下错误:
这是我的控制器:
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 等,并将其发送到数据库。我正在为 rails 使用回形针 gem。
尝试在您的表单标签中添加 multipart/form-data
属性。当您使用具有文件上传控件的表单时需要此值
<form class="margin-clear" role="form" method="post" action="careers#create" enctype="multipart/form-data">