Ruby Rails回形针-在另一个字段中复制附件文件名
我有下表:Ruby Rails回形针-在另一个字段中复制附件文件名,ruby,ruby-on-rails-4,file-upload,parameters,paperclip,Ruby,Ruby On Rails 4,File Upload,Parameters,Paperclip,我有下表: 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_
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
每当我使用回形针上传文件时,我都想将“附件文件名”的参数复制到“标题”
这个应用程序是一个API,但我正在使用一个debugs\u控制器来测试它
调试控制器
class DebugsController < ApplicationController
def index
@document = Document.new
@documents = Document.all
@subject = Subject.new
end
end
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
调试/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 DebugsController < ApplicationController
def index
@document = Document.new
@documents = Document.all
@subject = Subject.new
end
end
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
这将为我输出“nil:NilClass”的未定义方法“original_filename”
参数:
{"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}
有人知道如何将“附件文件名”的内容复制到文档的“标题”字段中吗?而且在复制文件名的过程中,是否可以删除上载文件的扩展名?您可以在模型中而不是在控制器中执行此操作
class Document < ActiveRecord::Base
before_save :set_title
def set_title
self.title = self.attachment_file_name
end
...
end
这帮我找到了正确的方向。我最终使用了“before_create”,因此仅用于新对象的创建操作,这样我就可以在保存文档后将标题更新为其他内容。附件\u文件\u名称中的“self”不是必需的。非常感谢。您可以使用ruby alias attributes方法进行分配: