# Rails, Paperclip and SWFupload

**URL:** <https://www.ruby-forum.com/t/rails-paperclip-and-swfupload/188811>\
**Category:** Rails\
**Created:** [May 18, 2010, 8:11pm UTC](https://www.ruby-forum.com/t/rails-paperclip-and-swfupload/188811 "2010-05-18T20:11:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ruby\_on\_RailsS\_Talk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/dbc845/32.png) [@Ruby\_on\_RailsS\_Talk](https://www.ruby-forum.com/u/Ruby_on_RailsS_Talk)\
**Post date:** [May 18, 2010, 8:11pm UTC](https://www.ruby-forum.com/t/rails-paperclip-and-swfupload/188811/1 "2010-05-18T20:11:40Z")

</div>

Hi

I’m sure this is in the wrong group - so any suggestions for a right  
group would be great! And you have my express permission to treat me  
like an idiot in replies! I have a fair amount of knowledge …  
but … obviously not enough to understand this past the basics!

I have two problems … but only going to list one for now …

I’m trying to use the Jim N. code and examples with swfupload and  
not having much success.

I’m using Rails 2.3.5 if that’s any use.

I’ve already had to do this in my PhotosController:

skip\_before\_filter :verify\_authenticity\_token, :only =\> :create

just to get the app to ignore {play nicely?) with the  
InvalidAuthenticityToken problems! And I promise I’ve read EVERY post  
there is on the problem from Google and beyond. I’ve changed more code  
try to solve the InvalidAuthenticityToken than I think Microsoft have  
ever produced … and still no help. More on this in another post …

But back to my problem:

I have this as part of my photo controller code:

def create  
if params[:Filedata]  
@photo = TripPhoto.new(:swfupload\_file =\> params[:Filedata])  
if @photo.save  
render :partial =\> ‘photo’, :object =\> @photo  
else  
render :text =\> “error”  
end  
else … [rest omitted]

The data is being passed to the controller I think from the log …

Processing TripPhotosController#create (for 127.0.0.1 at 2010-05-17  
21:24:26) [POST]  
Parameters: {“Filename”=\>“SYTS Logo Colour - All Red.png”,  
“trip\_id”=\>“3”, “action”=\>“create”, “authenticity\_token”=\>"[Omitted]",  
“Upload”=\>“Submit Query”, “controller”=\>“trip\_photos”,  
“\_worldtripper\_session”=\>"[Omitted]", “Filedata”=\>#\<File:/tmp/  
RackMultipart20100517-44821-hoknr8-0\>}

But I get the following error …

NoMethodError (undefined method `file=' for #<TripPhoto:0x103d99410>): app/models/trip_photo.rb:13:in`swfupload\_file=’

I have inserted (as per Jim’s instructions) the following into my  
TripPhoto Class…

require ‘mime/types’

class TripPhoto \< ActiveRecord::Base

belongs\_to :trip  
has\_attached\_file :photo, :styles =\> { :small =\> “150x150\>”, :large  
=\> “320x240\>” }  
validates\_attachment\_presence :photo  
validates\_attachment\_size :photo, :less\_than =\> 5.megabytes

def swfupload\_file=(data)  
data.content\_type =  
MIME::Types.type\_for(data.original\_filename).first.content\_type  
logger.warn(“Data content type is: #{data.content\_type}”)  
self.file = data  
end

end

So I have a feeling it has something to do with the line:

self.file = data

But I really have NO IDEA what is wrong! I’ve spent days on this and  
it’s not through lack of trying …

So any help please would be fabulous!

Thanks

Darren

Oh … BTW …

If I make the line

@photo = TripPhoto.new(params[:Filedata])

… I end up with the following error:

NoMethodError (undefined method `stringify_keys!' for #<File:/tmp/ RackMultipart20100517-44821-hoknr8-0>): app/controllers/trip_photos_controller.rb:20:in`new’

Which makes even less sense …

---

<div class="post-metadata">

**Author:** ![Ruby\_on\_RailsS\_Talk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/dbc845/32.png) [@Ruby\_on\_RailsS\_Talk](https://www.ruby-forum.com/u/Ruby_on_RailsS_Talk)\
**Post date:** [May 18, 2010, 8:42pm UTC](https://www.ruby-forum.com/t/rails-paperclip-and-swfupload/188811/2 "2010-05-18T20:42:43Z")

</div>

Hi

I solved it … but have no idea why!

Needed to change:

self.file = data

to

self.photo = data

And it works … hmmmm …

On May 18, 7:10 pm, “Ruby on Rails: Talk” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Ruby\_on\_RailsS\_Talk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/dbc845/32.png) [@Ruby\_on\_RailsS\_Talk](https://www.ruby-forum.com/u/Ruby_on_RailsS_Talk)\
**Post date:** [May 18, 2010, 8:52pm UTC](https://www.ruby-forum.com/t/rails-paperclip-and-swfupload/188811/3 "2010-05-18T20:52:05Z")

</div>

On May 18, 2010, at 11:41 AM, Ruby on Rails: Talk wrote:

> self.photo = data
> 
> And it works … hmmmm …

Because your Photo model “has\_attached\_file :photo, …” _NOT_  
“has\_attached\_file :file, …”

-philip

---

<div class="post-metadata">

**Author:** ![Ruby\_on\_RailsS\_Talk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/dbc845/32.png) [@Ruby\_on\_RailsS\_Talk](https://www.ruby-forum.com/u/Ruby_on_RailsS_Talk)\
**Post date:** [May 18, 2010, 9:03pm UTC](https://www.ruby-forum.com/t/rails-paperclip-and-swfupload/188811/4 "2010-05-18T21:03:13Z")

</div>

On May 18, 2010, at 11:10 AM, Ruby on Rails: Talk wrote:

> not having much success.  
> try to solve the InvalidAuthenticityToken than I think Microsoft have  
> ever produced … and still no help. More on this in another post …

I recently configured swfupload with Rails using the beta3 of swfupload  
so I could have it to client side image resizing.

Take a look at this rails demo app:

[http://philip.pjkh.com/swfupload-rails-resize-demo.tgz](http://philip.pjkh.com/swfupload-rails-resize-demo.tgz)

It’s everything you need to get it going. If you don’t want the client  
side image resizing, just comment out the bits that invoke it. The  
swfupload handlers are setup to simply print out debugging information  
so you can see when/why/where they get called.

Just run ‘rake db:migrate’ to setup the sqlite database.

In particular though… look at these files/lines…

app/middleware/flash\_session\_cookie\_middleware.rb  
config/environment.rb - 2nd to last line  
config/initializers/session\_store.rb - last line  
app/models/photo.rb  
app/controllers/home\_controller.rb  
app/views/home/index.html.erb  
public/javascripts/swfupload\_handlers.js

-philip

---

<div class="post-metadata">

**Author:** ![Ruby\_on\_RailsS\_Talk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/dbc845/32.png) [@Ruby\_on\_RailsS\_Talk](https://www.ruby-forum.com/u/Ruby_on_RailsS_Talk)\
**Post date:** [May 18, 2010, 9:17pm UTC](https://www.ruby-forum.com/t/rails-paperclip-and-swfupload/188811/5 "2010-05-18T21:17:51Z")

</div>

Thanks so much for you help with the problem and the suggestion with  
new version of swfupload!

I don’t suppose you have any clear insight into why I can’t get the  
InvalidAuthorizationToken thing to work do you?

Thanks once again for help already given and any more you might be  
able to offer

Darren

---

<div class="post-metadata">

**Author:** ![Ruby\_on\_RailsS\_Talk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/dbc845/32.png) [@Ruby\_on\_RailsS\_Talk](https://www.ruby-forum.com/u/Ruby_on_RailsS_Talk)\
**Post date:** [May 18, 2010, 9:41pm UTC](https://www.ruby-forum.com/t/rails-paperclip-and-swfupload/188811/6 "2010-05-18T21:41:52Z")

</div>

> Thanks so much for you help with the problem and the suggestion with  
> new version of swfupload!
> 
> I don’t suppose you have any clear insight into why I can’t get the  
> InvalidAuthorizationToken thing to work do you?

My guess is you’re not passing it along via Flash. See this bit in  
app/views/home/index.html.erb where I invoke the swfupload object. See  
where it’s passing the session key and the authenticity token? That’s  
important. And also why you need that middleware stuff.

$(document).ready(function() {  
swfu = new SWFUpload({  
upload\_url : “\<%= upload\_path %\>”,  
file\_post\_name : “Filedata”,  
post\_params : {  
“\<%= key =  
ActionController::Base.session\_options[:key] %\>” : “\<%= cookies[key]  
%\>”,  
“\<%=  
request\_forgery\_protection\_token %\>” : “\<%= form\_authenticity\_token  
%\>”  
},

> Thanks once again for help already given and any more you might be  
> able to offer

No problem!

-philip

---

<div class="post-metadata">

**Author:** ![Ruby\_on\_RailsS\_Talk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/dbc845/32.png) [@Ruby\_on\_RailsS\_Talk](https://www.ruby-forum.com/u/Ruby_on_RailsS_Talk)\
**Post date:** [May 19, 2010, 12:02am UTC](https://www.ruby-forum.com/t/rails-paperclip-and-swfupload/188811/7 "2010-05-19T00:02:22Z")

</div>

On 18 May 2010, at 21:40, Philip H. wrote:

> ```
> "<%= key =  
> 
> ```
> 
> ActionController::Base.session\_options[:key] %\>" : “\<%= cookies[key]  
> %\>”,  
> “\<%=  
> request\_forgery\_protection\_token %\>” : “\<%=  
> form\_authenticity\_token %\>”  
> },

You can also just include swfupload.cookies.js (it’s in the plugins  
folder when you download SWFupload from the site and you just include  
it after swfupload.js) and never worry about passing in the right  
cookies, it will add them to the post parameters automatically.

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![Ruby\_on\_RailsS\_Talk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/dbc845/32.png) [@Ruby\_on\_RailsS\_Talk](https://www.ruby-forum.com/u/Ruby_on_RailsS_Talk)\
**Post date:** [May 19, 2010, 12:09am UTC](https://www.ruby-forum.com/t/rails-paperclip-and-swfupload/188811/8 "2010-05-19T00:09:59Z")

</div>

> You can also just include swfupload.cookies.js (it’s in the plugins folder when you download SWFupload from the site and you just include it after swfupload.js) and never worry about passing in the right cookies, it will add them to the post parameters automatically.

Good to know. That won’t help with the authenticity token though…  
But definitely handy if you need more cookies than just the session!

-philip

---

<div class="post-metadata">

**Author:** ![Ruby\_on\_RailsS\_Talk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/dbc845/32.png) [@Ruby\_on\_RailsS\_Talk](https://www.ruby-forum.com/u/Ruby_on_RailsS_Talk)\
**Post date:** [May 19, 2010, 10:00am UTC](https://www.ruby-forum.com/t/rails-paperclip-and-swfupload/188811/9 "2010-05-19T10:00:04Z")

</div>

On 19 May 2010, at 00:08, Philip H. wrote:

> Good to know. That won’t help with the authenticity token though…

Indeed, our swfuploads are completely separated into reusable  
Javascript objects, so I didn’t think of it anymore.

Note to self: don’t answer mailing list replies while half asleep 🙂

Best regards

Peter De Berdt
