繁体   English   中英

使用paperclip将pdf上传到rails无法正常工作

[英]Uploading a pdf to rails using paperclip not working

我正在使用paperclip将pdf上传到我的数据库。 但是PDF没有上传到数据库中,它只是创建一个空白条目,如下所示:

      2.1.0 :001 > YearlyGuide.last
         YearlyGuide Load (1.4ms)  SELECT  "yearly_guides".* FROM "yearly_guides"   ORDER       BY "yearly_guides"."id" DESC LIMIT 1
                => #<YearlyGuide id: 2, pdf_file_name: nil, pdf_content_type: nil, pdf_file_size: nil, pdf_updated_at: nil, start: nil, end: nil, created_at: "2014-06-09 14:10:50", updated_at: "2014-06-09 14:10:50"> 

我的控制器看起来像这样:

   def new
@yearguide = YearlyGuide.new

结束

 def create
@yearguide = YearlyGuide.create
if @yearguide.save
  redirect_to '/' 
else
  render 'new'
end

结束

我的模特:

   class YearlyGuide < ActiveRecord::Base
has_attached_file :pdf
    validates_attachment :pdf, content_type: { content_type: "application/pdf" }
    end

我的new.html.erb这个:

     <%= bootstrap_form_for(@yearguide, layout: :horizontal, label_col: "col-sm-2", control_col: "col-sm-10") do |f| %>
<%= f.date_select :end %>

<%= f.file_field :pdf, help: "Ensure images are minimum 400x400px"  %>
<%= f.submit "Add Event", class: "btn btn-primary" %>

为什么文件没有上传? 我错过了什么? 我的代码有错误吗?

Rails 3

def create
  @yearguide = YearlyGuide.new(params[:yearly_guide])
  if @yearguide.save
    redirect_to '/' 
  else
    render 'new'
  end
end

Rails 4

def create
  @yearguide = YearlyGuide.new(yearly_guide_params)
  if @yearguide.save
    redirect_to '/' 
  else
    render 'new'
  end  
end

def yearly_guide_params
  params.require(:yearly_guide).permit(:pdf)
end

简而言之,你使用.create但没有传递任何参数,因为你有if @yearguide.save你想要新的。 如果你真的想在我上面的答案中创建create in sub - 留下params部分。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM