Is it possible to include the username in an attachment file_name? Using Paperclip, Rails 3., Devise

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}”)
endend

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
endend

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.

On Oct 1, 2013, at 3:40 PM, Monserrat F. [email protected]
wrote:

I think besides the obvious issue, it has to do bit

Well, no, it more likely has to do with the line of code pointed out in
the error message :wink:


Scott R.
[email protected]
http://www.elevated-dev.com/
(303) 722-0567 voice

lol. I meant, If i replace self.inventory.instance_write(:
inventory_file_name, “#{:current_user}#{extension}”) for

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

the error disappears but, the file_name doesn’t change. it seems to be
saving as
:rails_root/tmp/uploaded_files/inventories/:basename_.:extension
ignoring the self.inventory.instance_write(:inventory_file_name,
“#{SecureRandom.hex(16)}#{extension}”) line

I read this:

http://stackoverflow.com/questions/1146848/generating-a-unique-file-path-with-polymorphic-paperclip

and it could work, but I have no idea how to do have a reference in your
model to user table (say user_id), you can do sth like that
attachment.instance.user_id

Could someone please give me an example?

On Oct 1, 2013, at 4:27 PM, Monserrat F. [email protected]
wrote:

self.inventory.instance_write(:inventory_file_name,
“#{SecureRandom.hex(16)}#{extension}”)
the error disappears but, the file_name doesn’t change. it seems to be saving as
:rails_root/tmp/uploaded_files/inventories/:basename_.:extension ignoring the
self.inventory.instance_write(:inventory_file_name,
“#{SecureRandom.hex(16)}#{extension}”) line

Are you also removing the assignment to current_user when you do that?
Because that’s where the error comes from.

What is inventory_file_name? Why do you expect that setting it will
actually set the file name?


Scott R.
[email protected]
http://www.elevated-dev.com/
(303) 722-0567 voice

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs