我有下表:
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'的参数。到标题'。
这个应用程序是一个API,但我使用debugs_controller来测试它。
DebugsController
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>
DocumentsController
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
Document.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
这输出未定义的方法`original_filename&#39;为零:NilClass&#39;对我来说。
PARAMS:
{"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}
有谁知道如何复制&_ 39; attachment_file_name&#39;的内容?到了标题&#39;文件领域?也;是否可以在复制文件名的过程中删除上传文件的扩展名?
答案 0 :(得分:0)
您可以在模型中而不是在控制器中执行此操作。
class Document < ActiveRecord::Base
before_save :set_title
def set_title
self.title = self.attachment_file_name
end
...
end