Access to session variable in model

Hi everyone

I am using attachment fu for image uploading. I try to customize the
path for pictures be saved.

If I call session[:kwikerhash] in the image.rb where there method for
path definition is I get an undefined method or variable error. If I
fill a global variable @kwikerhash in the controller with the session
value and try to use it in the image.rb it says that @kwikerhash is nil.

Is there a possibility to get access to session values in model files?

Thanks in advance.

Regards

Adam

Hi Adam M.

Is there a possibility to get access to session values in model files?

You can’t do that and if any means doing that means violating MVC .
What you can do is to pass the session variable from controller to model
For example in the image.rb you have a

def do_this(session_id)

end

So from controller you can pass it like

@image_model_obj.do_this(session[:kwikerhash])

Sijo

Sijo Kg wrote:

Hi Adam M.

Is there a possibility to get access to session values in model files?

You can’t do that and if any means doing that means violating MVC .
What you can do is to pass the session variable from controller to model
For example in the image.rb you have a

def do_this(session_id)

end

So from controller you can pass it like

@image_model_obj.do_this(session[:kwikerhash])

Sijo

Hi Sijo

you’re right. But if I set a value to @kwikerhash in the controller I
have also access to it in the view.

This is fucked. Because its a method which I override from the
attachment_fu plugin.

So I need to create the same method with parameters.

Thanks

On your image model, you could add, for example:

class Image < ActiveRecord::Base
attr_writer :session_date

and, on you controller, right after you create a new image:

@image = Image.new <whatever_parameters>
@image.session_date = session[:kwikerhash][<whatever_param>]

Hope this helps!

On Sep 30, 6:54 pm, Adam M. [email protected]

Hello again,

sorry but I have no Idea how to solve it. I try to explain the problem.
I am using attachment_fu for picture upload and output. I wanna change
the attachment-fu to save the pics in a specific folder. Therefor my
image.rb looks

class Image < ActiveRecord::Base
has_many :categories
belongs_to :category
belongs_to :products
belongs_to :kwikers

has_attachment :content_type => :image,
:storage => :file_system, :path_prefix => ‘public/assets/uploads’,
:max_size => 10.megabytes,
:resize_to => ‘1024x1024>’,
:thumbnails => { :thumb => ‘150x150>’},
:processor => :MiniMagick # attachment_fu looks in this order:
ImageScience, Rmagick, MiniMagick
validates_as_attachment

def uploaded_data=(file_data)
return nil if file_data.nil? || file_data.size == 0
self.content_type = file_data.content_type
extension = file_data.original_filename.slice(/.\w+$/)

 self.filename = Digest::SHA1.hexdigest(Time.now.to_s)  + extension

 File.extname(file_data.original_filename) if respond_to?(:filename)
 if file_data.is_a?(StringIO)
   file_data.rewind
   self.temp_data = file_data.read
 else
     self.temp_path = file_data.path
 end

end

START: FOLDER STRUCTURE MODIFYING CODE

Changes the folder structure

def full_filename(thumbnail = nil)
file_system_path = (thumbnail ? thumbnail_class :
self).attachment_options[:path_prefix]
File.join(RAILS_ROOT, file_system_path,
created_at.strftime("%Y/%m/%d"), thumbnail_name_for(thumbnail))
end
end

I want to replace the created_at.strftime("%Y/%m/%d") in the
full_filename method with the value which is set in
session[:kwikerhash]. But I don’t know how. The view file calls a method
image.public_filename(:thumb) for example.

What can I do to let the full_filename method take the value from the
session?

Please help.

Adam

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