amazon s3 - Rails + Paperclip + AWS S3 has_attached_file method: Why doesn't this String interpolation work? -
here have piece of code should work based on know of string interpolation in ruby. inside model class: "s3_file"
basically wahat trying accomplish while saving file aws s3, save them under folder thats created @ runtime using following string interpolations. using devise , cancan authorization , authentication gems
the code below works:
paperclip.interpolates :prefix |attachment, style| "#{date.today.to_s }/system" end has_attached_file( :upload, :path => ":prefix/:basename.:extension", :storage => :s3, :s3_credentials => {:access_key_id => "xxx", :secret_access_key => "xxx"}, :bucket => "xxx" ) but, trying usersemail , insert paperclip block. code doesn't work desired. result of code not exception @curr_user_email null , result folder on aws s3 has no name. method create folder. how can correct this?
this code still not work
if(@curr_user_signed_in) @aprefix = "#{date.today.to_s }/#{@curr_user_email}" else @aprefix = "#{date.today.to_s }/system" end paperclip.interpolates :prefix |attachment, style| @aprefix end has_attached_file( :upload, :path => ":prefix/:basename.:extension", :storage => :s3, :s3_credentials => {:access_key_id => "xxx", :secret_access_key => "xxx"}, :bucket => "xxx" ) in controller have bit of code:
def index @s3_files = s3file.all @curr_user_signed_in = false if(user_signed_in?) @curr_user_signed_in = true @curr_user_email = current_user.email end respond_to |format| format.html # index.html.erb format.json { render json: @s3_files } end end so real problem these @curr_user_signed_in = true @curr_user_email = current_user.email being set , not null, reason cannot read paperclip block
looks need use paperclip interpolates. credit goes fellow:
https://stackoverflow.com/a/852768/931209
and here's snippet believe should solve problem.
# interpolate in paperclip paperclip.interpolates :maybe_user |attachment, style| @prefix = "#{date.today.to_s }/system/" if(user_signed_in?) { @prefix = "#{date.today.to_s }/#{current_user.id}/" } @prefix end then...
has_attached_file(:upload, :path => ":maybe_user/:basename.:extension", :storage => :s3, :s3_credentials => {:access_key_id => "xxxxx", :secret_access_key => "xxx"}, :bucket => "xxx" ) here's doc on paperclip website:
https://github.com/thoughtbot/paperclip/wiki/interpolations
hope helps!
edit: if gives shit, add .to_s strings , think should alright. though, don't know why would.
update -- 7/7 -- content starts below
preface: have not used devise.
the current_user method devise cannot accessed within model. it's available in controllers.
https://github.com/plataformatec/devise/blob/master/lib/devise/controllers/helpers.rb#l33
you'll need add attr_accessor model (s3file?) current user, , reference instance variable within model.
controller:
class s3filecontroller < applicationcontroller def create s = s3file.new if user_signed_in? s.current_user = current_user else s.current_user = nil end # alternatively written # user_signed_in? ? s.current_user = current_user : s.current_user = nil s.upload = params[:file] s.save end end model:
class s3file < activerecord::base attr_accessor :current_user paperclip.interpolates :maybe_user |attachment, style| @prefix = "#{date.today.to_s }/system/" if(@current_user) { @prefix = "#{date.today.to_s }/#{current_user.id}/" } @prefix end end i believe should work paperclip isn't processing file before s3file saved. have access user object via @current_user can interpolation:
paperclip.interpolates :prefix |attachment, style| @pre = "#{date.today.to_s}/system" if(@current_user) @pre = "#{date.today.to_s}/#{@current_user.email}" end @pre end a couple of things note:
1.) magic attr_accessor :current_user allows store value of current_user object s3file. can add many other attr_accessors you'd if want store more information.
2.) not there may not use case it, speaking, don't want access methods available controllers within model... sort of breaks principles of mvc, authentication should done in controllers, not in model. own experience having tried long ago authlogic , (failed) results. ymmv, it's food thought.
3.) i'm pretty sure have interpolation inside paperclip block. there isn't of reason not either.
[addendum]
4.) instance variables set in controller not available models. scoped class they're created in... why used attr_accessor =)
[/addendum]
hopefully gets few steps closer! luck.
Comments
Post a Comment