【问题标题】:Rails Paperclip - Duplicating the attachment file name in another fieldRails Paperclip - 在另一个字段中复制附件文件名
【发布时间】:2014-12-02 13:08:30
【问题描述】:

我有下表:

create_table "documents", force: true do |t|
  t.string   "title"
  t.integer  "subject_id"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.string   "attachment_file_name"
  t.string   "attachment_content_type"
  t.integer  "attachment_file_size"
  t.datetime "attachment_updated_at"
end

每当我上传文件(使用 Paperclip)时,我都想将 'attachment_file_name' 的参数复制到 'title'。

此应用是一个 API,但我正在使用 debugs_controller 对其进行测试。

调试控制器

class DebugsController < ApplicationController
   def index
    @document = Document.new
    @documents = Document.all
    @subject = Subject.new
  end
end

调试/index.html.erb

<form action="http://0.0.0.0:3000/documents" method="POST" enctype="multipart/form-data">
  <input name="document[title]" type="text" placeholder="doc naam">
  <input name='document[attachment]' type="file">
  <input name="document[subject_id]" type="text" placeholder="Subject id">
  <input type="submit">
</form>

文档控制器

class DocumentsController < ApplicationController
  before_action :set_document, only: [:show, :update, :destroy]
  skip_before_action :verify_authenticity_token

  respond_to :json

  def index
    @documents = Document.all
  end

  def new
    @document = Document.new
    @documents = Document.all
  end

  def show
  end

  def create
    @document = Document.new(document_params)
    @document.title = params[:attachment].original_filename

    if @document.save
      respond_with(@document, status: :created)

    else
      respond_with(@document, status: 403)
    end
  end

  def update
   if @document.update(document_params)
      respond_with(@document)
    else
      respond_with(@document.errors, status: :unprocessable_entity)
    end
  end

  def destroy
    @document.destroy
    respond_with(@document, status: :destroyed)
  end

  private

  def set_document
    @document = Document.find(params[:id])
  end

  def document_params
    params.require(:document).permit(:title, :attachment, :subject_id)
  end
end

文档.rb

class Document < ActiveRecord::Base
  belongs_to :subject
  has_many :notes
  has_attached_file :attachment, url: '/system/:attachment/:id_partition/:basename.:extension'

  validates_attachment :attachment, presence: {
    message: 'You have to upload a file!' }, content_type: {
      content_type: %w( application/pdf ), message: 'PDF files only.' }, size: {
        in: 0..10.megabytes, message: 'The maximum file-size is 10MB.' }

  validates :subject_id, presence: { message: 'Assign your document to a case!' }
end

这会为我输出 'undefined method `original_filename' for nil:NilClass'。

参数:

 {"document"=>{"title"=>"",
 "attachment"=>#<ActionDispatch::Http::UploadedFile:0x007fbcea87c518 @tempfile=#<Tempfile:/var/folders/sy/9v_t59x974qg_m2nvmcyvkjr0000gn/T/RackMultipart20141202-14285-z5aj46>,
 @original_filename="Teamsweet.pdf",
 @content_type="application/pdf",
 @headers="Content-Disposition: form-data; name=\"document[attachment]\"; filename=\"Teamsweet.pdf\"\r\nContent-Type: application/pdf\r\n">,
 "subject_id"=>"1"},
 "format"=>:json}

有谁知道如何将“附件文件名”的内容复制到文档的“标题”字段?还;复制文件名的过程中是否可以去掉上传文件的扩展名?

【问题讨论】:

    标签: ruby ruby-on-rails-4 file-upload parameters paperclip


    【解决方案1】:

    您可以在模型中而不是在控制器中执行此操作。

    class Document < ActiveRecord::Base
      before_save :set_title
    
      def set_title
        self.title = self.attachment_file_name
      end
    
      ...
    end
    

    【讨论】:

    • 这帮助我找到了正确的方向。我最终使用了'before_create'(因此仅适用于新对象(创建操作)),因此我可以在保存文档后将标题更新为其他内容。 attachment_file_name 中的“self”不是必需的。谢谢!
    • 你可以使用 ruby​​ 别名属性方法进行赋值:)
    猜你喜欢
    • 2013-12-13
    • 1970-01-01
    • 2015-11-22
    • 2017-11-07
    • 1970-01-01
    • 2019-04-22
    • 2011-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多