Monserrat Foster
2013-Oct-01 21:40 UTC
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. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
Scott Ribe
2013-Oct-01 21:51 UTC
Re: Is it possible to include the username in an attachment file_name? Using Paperclip, Rails 3., Devise
On Oct 1, 2013, at 3:40 PM, Monserrat Foster <monsefoster-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I think besides the obvious issue, it has to do bitWell, no, it more likely has to do with the line of code pointed out in the error message ;-) -- Scott Ribe scott_ribe-ZCQMRMivIIdUL8GK/JU1Wg@public.gmane.org http://www.elevated-dev.com/ (303) 722-0567 voice -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1AB90406-3EF6-4113-A3E4-B15F10DD6463%40elevated-dev.com. For more options, visit https://groups.google.com/groups/opt_out.
Monserrat Foster
2013-Oct-01 22:02 UTC
Re: Is it possible to include the username in an attachment file_name? Using Paperclip, Rails 3., Devise
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 On Tuesday, October 1, 2013 5:21:03 PM UTC-4:30, Scott Ribe wrote:> > On Oct 1, 2013, at 3:40 PM, Monserrat Foster <monse...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org<javascript:>> > 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 ;-) > > -- > Scott Ribe > scott...-ZCQMRMivIIdUL8GK/JU1Wg@public.gmane.org <javascript:> > http://www.elevated-dev.com/ > (303) 722-0567 voice > > > > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/e603ada4-9a67-4c6f-a3ff-13ad9dda027c%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Monserrat Foster
2013-Oct-01 22:27 UTC
Re: Is it possible to include the username in an attachment file_name? Using Paperclip, Rails 3., Devise
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 Tuesday, October 1, 2013 5:32:00 PM UTC-4:30, Monserrat Foster wrote:> > 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 > > > On Tuesday, October 1, 2013 5:21:03 PM UTC-4:30, Scott Ribe wrote: >> >> On Oct 1, 2013, at 3:40 PM, Monserrat Foster <monse...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 ;-) >> >> -- >> Scott Ribe >> scott...-ZCQMRMivIIdUL8GK/JU1Wg@public.gmane.org >> http://www.elevated-dev.com/ >> (303) 722-0567 voice >> >> >> >> >>-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/9518e3c3-c61a-47c4-8fac-aa40d948936b%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Scott Ribe
2013-Oct-01 23:03 UTC
Re: Is it possible to include the username in an attachment file_name? Using Paperclip, Rails 3., Devise
On Oct 1, 2013, at 4:27 PM, Monserrat Foster <monsefoster-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 Tuesday, October 1, 2013 5:32:00 PM UTC-4:30, Monserrat Foster wrote: > 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}") lineAre 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 Ribe scott_ribe-ZCQMRMivIIdUL8GK/JU1Wg@public.gmane.org http://www.elevated-dev.com/ (303) 722-0567 voice -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/F083E3FB-228F-4EF4-AF08-27EBF3D02CCE%40elevated-dev.com. For more options, visit https://groups.google.com/groups/opt_out.