Ruby on Rails Tuesday, October 1, 2013

Hello, I'm new to Ruby, Rails, Devise and Paperclip and I'm trying to change a filename when I upload it to include the user's nickname after a _ however, I keep getting filename.extension_(missing user name) and this error

    undefined method `current_user=' for #<UploadFiles:0x467ea88>  app/controllers/upload_files_controller.rb:31:in `create'

My code:

Model:

class UploadFiles < ActiveRecord::Base    before_create :change_file_name_inventory    before_create :change_file_name_material_list    attr_accessible :inventory, :material_list, :current_user    has_one :inventory    has_one :material_list    has_attached_file :inventory, :url=>"/tmp/inventoriy", :path=>":rails_root/tmp/inventories/:basename_.:extension"    has_attached_file :material_list, :url=>"/tmp/material_list", :path=>":rails_root/tmp/material_lists/:basename_.:extension"    accepts_nested_attributes_for :material_list, :allow_destroy => true      accepts_nested_attributes_for :inventory, :allow_destroy => true          private      def change_file_name_inventory      extension = File.extname(inventory_file_name).downcase      self.inventory.instance_write(:inventory_file_name, "#{:current_user}#{extension}")    end
  def change_file_name_material_list      extension = File.extname(material_list_file_name).downcase      self.material_list.instance_write(:material_list_file_name, "#{:current_user}#{extension}")    end  end

Controller:

class UploadFilesController < ApplicationController
  before_filter :authenticate_user!    # GET /uploads/new    # GET /uploads/new.json    def new      @upload_files = UploadFiles.new      @upload_files.current_user = current_user.email      respond_to do |format|        format.html # new.html.erb        format.json { render json: @upload_files }      end    end      def create      @upload_files = UploadFiles.create(params[:upload_files])      @upload_files.current_user = current_user.email      respond_to do |format|        if @upload_files.save        else          format.html { render action: "new" }          format.json { render json: @upload_files.errors, status: :unprocessable_entity }        end      end    end  end

the question is, how can I do to upload the filenames as inventory_name_file_username.extension and material_list_file_name_username.extension

and if there's any way of avoid two methods (one for each attachment), by making just one that appends the username at the end of the file_name... I'm new to Ruby, Rails, Devise and Paperclip and I have no idea how to do this. I've read about it (changing file_name to random, include a date and such) and tried to fix it according to it, but no luck so far.

Why i'm doing this:

A user can upload one inventory and one material list, when it does, a new file is generated after processing the uploaded files, then, the user downloads the generated file and if the user uploads a new inventory and/or a new material list, it's overwritten, because I don't want the server to be full of unnecesary files (the inventory changes everyday and the material list is rarely the same). So, this way, if a user 'X' uploads an inventory file today and it uploads several material lists, only the material lists are overwritten each time, however another user 'Y' may be using the system and the same time, and if user 'Y' uploads a different inventory, I don't want the inventory user 'x' uses to be overwritten.

EDIT:

I think besides the obvious issue, it has to do bit :path=>":rails_root/tmp/uploaded_files/inventories/:basename_.:extension"

because I tried to just append a random number and it still takes the name_.extension although the random number seems to be right and "should" work ...

self.inventory.instance_write(:inventory_file_name, "#{SecureRandom.hex(16)}#{extension}")

took it from here: http://trevorturk.com/2009/03/22/randomize-filename-in-paperclip/


I'd appreciate any help you're able to offer me. Thank you in advance. 

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ad0b1524-6937-4fd6-bc8e-7f951f7ff9a7%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment