ruby - Rails Paperclip - Duplicating the attachment file name in another field -


i've got following table:

create_table "documents", force: true |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 

whenever upload file (using paperclip), want duplicate param of 'attachment_file_name' 'title'.

this app api, i'm using debugs_controller test it.

debugscontroller

class debugscontroller < applicationcontroller    def index     @document = document.new     @documents = document.all     @subject = subject.new   end end 

debugs/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 upload file!' }, content_type: {       content_type: %w( application/pdf ), message: 'pdf files only.' }, size: {         in: 0..10.megabytes, message: 'the maximum file-size 10mb.' }    validates :subject_id, presence: { message: 'assign document case!' } end 

this outputs 'undefined method `original_filename' nil:nilclass' me.

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} 

does know how can duplicate contents of 'attachment_file_name' 'title' field of documents? also; possible remove extention of uploaded file in process of duplicating file name?

you can in model instead of in controller.

class document < activerecord::base   before_save :set_title    def set_title     self.title = self.attachment_file_name   end    ... end 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -