# Problem with session

**URL:** <https://www.ruby-forum.com/t/problem-with-session/134528>\
**Category:** Rails\
**Created:** [April 11, 2008, 6:47pm UTC](https://www.ruby-forum.com/t/problem-with-session/134528 "2008-04-11T18:47:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/2acd7d/32.png) [@vash](https://www.ruby-forum.com/u/vash)\
**Post date:** [April 11, 2008, 6:47pm UTC](https://www.ruby-forum.com/t/problem-with-session/134528/1 "2008-04-11T18:47:45Z")

</div>

as my first project i am playing around with the swfupload ruby demo  
now i have run in a problem i dont get, maybe due to my lack of ror  
experience

what i want to do is, during upload, get the ids of the uploaded pics,  
put them in session[:new\_photos] and then use this information in  
another view to tag all the newly uploaded pics

swfupload uploads the files to this, one file after another, so the  
swfupload method gets called multiple times for multiple files

def swfupload  
# swfupload action set in routes.rb

```
# The content_type is set as application/octet by the flash program
# Use mimetype-fu to determine the mimetype before passing it
# to the attachment_fu enabled model
session[:new_photos] = session[:new_photos] || [] #<- i only did

```

this after session[:new\_photos] \<\< @photo.id returned nil  
params[:Filedata].content\_type =  
File.mime\_type?(params[:Filedata].original\_filename)  
@photo = Photo.new :uploaded\_data =\> params[:Filedata]  
@photo.save!  
debugger  
session[:new\_photos] \<\< @photo.id  
debugger  
# This returns the thumbnail url for handlers.js to use to display  
the thumbnail  
render :text =\> @photo.public\_filename(:thumb)  
rescue  
render :text =\> “Error”  
end

def x  
debugger  
@add\_tags = Photo.find(session[:new\_photos])  
debugger  
render :action =\> “edit\_multiple”  
end

after all files are uploaded i redirect to the x method which in turn  
renders the general method for adding tags

the strange thing i ran into is, that session is apparently only visible  
in the swfupload method, although i used it without problems in other  
places

when i tried to debug it first there was the problem that  
session[:new\_photos] was nil in the swfupload method, so i tried to  
initialize it in the first run through, that worked, although i could  
not access it in the x method because it was nil again (during the  
debugging it was clearly filled with some ids in the swfupload method)  
so i guess the way i did it somehow makes session a local array with  
scope only for swfupload  
but i dont know how else to do it

any suggestions would be appreciated, tia, alex

---

<div class="post-metadata">

**Author:** ![vash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/2acd7d/32.png) [@vash](https://www.ruby-forum.com/u/vash)\
**Post date:** [April 11, 2008, 7:07pm UTC](https://www.ruby-forum.com/t/problem-with-session/134528/2 "2008-04-11T19:07:32Z")

</div>

yep, swfupload, due to a bug in Flash, can’t send the information  
necessary for Rails session handling.  
there are several blogs here, that handle this theme (search)

basicly there are two ideas about it:  
manually send the session data with the file and somehow make Rails use  
it. last thing i heard about it was, that someone made it run with FF  
but not with IE.

my approach:  
turn off session handling for the upload function  
add one more step: send a upload\_request (ajax) for each file, create a  
record and store some random string, hand this back with the ajax  
response and add as parameter to swfupload:

this function will go through the files and request uploads:  
(for another bug it has to use the generated html (uploadQueue /  
file\_rows) to get the file\_ids)

function requestFileUploads() {  
queued\_files = $(“uploadQueue”).getElementsByClassName(“file\_row”)  
for(i = 0; i \< queued\_files.length; i++) {  
fileObject =  
swf\_upload\_control.getFile(queued\_files[i].getAttribute(“name”));  
new Ajax.Request("\<%= request\_upload\_products\_url %\>", {  
method: ‘get’,  
evalScripts: true,  
parameters: {  
file\_name: fileObject.name,  
file\_id: fileObject.id,  
document\_name: $(“input\_” + fileObject.id).value  
}  
});  
}  
}

this function is called by the response:

function queue\_acknowledged\_files(file\_id, document\_id, checksum) {  
swf\_upload\_control.addFileParam(file\_id, “document\_id”, document\_id);  
swf\_upload\_control.addFileParam(file\_id, “document\_checksum”,  
checksum);  
swf\_upload\_control.startUpload(file\_id);  
}

the upload function then looks for a document record with the given id  
and checksum, stores the file and deletes the checksum.

that’s not the complete code, but i think you can get the idea
