Errors when using paperclip plugin for rails in pr

2020-04-30 02:44发布

问题:

I am running rails 3 and paperclip 2.3.4. In development on OS X every thing runs great but production there are some issues that I can't quite figure out. If anyone have suggestions please let me know.

I am uploading the file using flash BTW. Like I said local development works great.

I checked the production log and it looks like the POST is happening just fine.

Started POST "/addimage" for 10.0.136.119 at 2011-01-13 19:49:09 +0000
  Processing by BowlsController#create as HTML
  Parameters: {"Filename"=>"IMG_0585.JPG", "fbid"=>"123456789", "Filedata"=>#<ActionDispatch::Http::UploadedFile:0x000000054afdf8 @original_filename="IMG_0585.JPG", @content_type="application/octet-stream", @headers="Content-Disposition: form-data; name=\"Filedata\"; filename=\"IMG_0585.JPG\"\r\nContent-Type: application/octet-stream\r\n", @tempfile=#<File:/tmp/RackMultipart20110113-23741-bhze8q>>, "Upload"=>"Submit Query"}
Completed 200 OK in 64ms (Views: 0.4ms | ActiveRecord: 0.9ms)

Here is the action in rails that I am calling

def create
     @user = User.find_by_fbid(params[:fbid])

     if @user.nil?
       #create user and register if user does not exist
       @user = User.new( :fbid => params[:fbid] )
       @user.save
     end

    if params[:Filedata]
      @image = @user.images.build()
      @image.swfupload_file = params[:Filedata]

      if @image.save
        render :json => { :data => @image, :success => true }
      else
        render :json => { :info => "error", :success => false }
      end
    else
      @image = Image.new params[:img]
      if @image.save
        render :json => { :data => @bowl, :success => true }
      else
        render :json => { :info => "error", :success => false }
      end
    end
  end
end

Basically a User can have many images so I have to make sure the User exists. If they do not exist the I create a new user first.

I Also have another method that allows me to grab the path to the last image the user uploads that is also returning an error in production.

  def last_img
    @user = User.find_by_fbid(params[:fbid])

    if @user.nil?
      render :json => { :error => "no bowls for this user" }
    else
      bowl = @user.images.first

      render :json => { :img_path => image.img.url}
    end
  end

the error that I am getting for this is the following NoMethodError in ImagesController#last_image

undefined method `img' for nil:NilClass

回答1:

I feel a little bit lost with your code.

If it really is a copy of your live code then the image.img probably would throw an exception in #last_img because it is probably not initialized (at least not in code you've provided). But it would raise a different kind of exception not a nil:NilClass

In case that it's a typo and you ment @image.img then the question is same - what is a workflow of your controller - if it's called as it is then it would really throw nil:NilClass because there is not @image initialized anywhere in the code of #last_img

And another thing - you are initializing local variable bowl there and there is no use of it?

And last one, now in #create - where did @bowl came from? From provided code it should be always nil.