2014-12-02 2 views
0

나는 다음 표를 가지고 : 나는 파일을 (종이 클립을 사용하여), I가 'attachment_file_name'의 PARAM을 복제 할 업로드 할 때마다레일 종이 클립 다른 필드에 첨부 파일 이름을 복제하는 것은

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_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 

에게

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 
,691,363 Document.rb를 디버깅210

이렇게하면 nil : NilClass '에 대해'정의되지 않은 메소드 'original_filename'이 출력됩니다.

에 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} 

사람이 나는 문서의 '제목'필드에 'attachment_file_name'의 내용을 복제 할 수있는 방법을 알고 있나요? 또한; 파일 이름을 복제하는 과정에서 업로드 된 파일의 확장자를 제거 할 수 있습니까?

답변

0

컨트롤러 대신 모델에서이 작업을 수행 할 수 있습니다.

class Document < ActiveRecord::Base 
    before_save :set_title 

    def set_title 
    self.title = self.attachment_file_name 
    end 

    ... 
end 
+0

올바른 방향으로 나를 도왔습니다. 나는 'before_create'를 사용하여 결국 (새로운 객체 (동작 만들기)에만 사용됨) 결국 문서를 저장 한 후에 제목을 다른 것으로 업데이트 할 수있었습니다. attachment_file_name의 'self'는 필요하지 않습니다. 고맙습니다! – Alserda

+0

당신은 할당을 위해 루비 별칭 속성 메소드를 사용할 수 있습니다 :) –

관련 문제