Rails, Paperclip and SWFupload ...

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 Neath 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 ...

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" <dazzaroo...@gmail.com>
wrote:

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

Hi

I solved it ... but have no idea why!

Needed to change:

self.file = data

to

self.photo = data

And it works ... hmmmm ...

Because your Photo model "has_attached_file :photo, ..." *NOT* "has_attached_file :file, ..."

-philip

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

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 Neath 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 ...

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

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

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

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

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

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 %>"
                                    },

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

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 :slight_smile:

Best regards

Peter De Berdt