title: NoMethodError (undefined method `new' for nil:NilClass): when try to create Photo object
body: <p>When I try to assign the data sent with the POST method to an object I get the following error.</p>
  
  <pre><code>Started POST "/backend/photos" for 127.0.0.1 at 2012-02-10 19:24:48 +0100
  Processing by Backend::PhotosController#create as */*
    Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"yeFZOu6sG3tyNqT4uBb5Xq0fp+1yTZ2wxn0WFPvGhPw=", "photo"=&gt;{"source_type"=&gt;"post", "source_id"=&gt;"5", "cropping"=&gt;"1", "img"=&gt;#&lt;ActionDispatch::Http::UploadedFile:0x00000004798020 @original_filename="p028.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"photo[img]\"; filename=\"p028.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#&lt;File:/tmp/RackMultipart20120210-27534-1yyq97g&gt;&gt;}}
  Completed 500 Internal Server Error in 1ms
  
  NoMethodError (undefined method `new' for nil:NilClass):
    app/controllers/backend/photos_controller.rb:5:in `new'
    app/controllers/backend/photos_controller.rb:5:in `create'
  
  
    Rendered /usr/local/rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.6ms)
    Rendered /usr/local/rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
    Rendered /usr/local/rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (5.3ms)
  </code></pre>
  
  <p>In controller:</p>
  
  <pre><code># -*- encoding : utf-8 -*-
  class Backend::PhotosController &lt; Backend::BaseController
  
    def create
      @photo = Photo.new(params[:photo])
  
      if @photo.save
        render :json =&gt; { :pic_path =&gt; @photo.img.url.to_s , :name =&gt; @photo.img.instance.attributes["img_file_name"] }, :content_type =&gt; 'text/html'
      else
        #todo handle error
        render :json =&gt; { :result =&gt; 'error'}, :content_type =&gt; 'text/html'
      end
    end
  </code></pre>
  
  <p>In model:</p>
  
  <pre><code>    # -*- encoding : utf-8 -*-
  
  class Photo &lt; ActiveRecord::Base
    scope :previous, lambda {|p| where("id &lt; ? ", p.id).order(" id desc ").limit(1) }
    scope :next, lambda {|p| where("id &gt; ? ", p.id).order(" id asc ").limit(1) }
  
    after_save :observe_seq_after_save
    before_create :set_seq_before_create
    after_update :change_seq_after_update
    after_destroy :change_seq_after_destroy
  
    attr_accessor :cropping
    belongs_to :product
    after_update :reprocess_picture, :if =&gt; :cropping?
  
    has_attached_file :img, :styles =&gt; {
      :thumb =&gt;  { :geometry =&gt; '130x130#', :quality =&gt; 60, :format =&gt; 'JPG', :processors =&gt; [:cropper]},
      :normal =&gt;  { :geometry =&gt; '520x520&gt;', :quality =&gt; 90, :format =&gt; 'JPG'},
      :big =&gt;  { :geometry =&gt; '800x600&gt;', :quality =&gt; 90, :format =&gt; 'JPG'}
    }
  
    validates_attachment_presence :img
    validates_attachment_size :img, :less_than =&gt; 5.megabytes
    validates_attachment_content_type :img, :content_type =&gt; [ 'image/jpeg', 'image/png', 'image/pjpeg' ]
  </code></pre>
  
  <p>In routes:</p>
  
  <pre><code>  namespace :backend do
        root :to =&gt; "dashboards#show"  
  
        match "/set_per_page_admin" =&gt; "sessions#set_per_page_admin"
        match "/posts/:post_id/photos/:id/move_up" =&gt; "photos#move_up"
        match "/posts/:post_id/photos/:id/move_down" =&gt; "photos#move_down"
        match "/photos/reset_seq" =&gt; "photos#reset_seq"
  
        match "/photos/new/:source_type/:source_id" =&gt; "photos#new"
        resources :photos do
          member do
            get :crop
            get :crop_existing_photo
          end
        end
  
  
        scope "/remote" do
          match "/show_pic" =&gt; "photos#show_pic"
          match "/show_crop" =&gt; "photos#show_crop"
        end
  
        resources :posts
    end
  </code></pre>
  
  <p>Installed gems:</p>
  
  <pre><code>Gems included by the bundle:
    * actionmailer (3.2.1)
    * actionpack (3.2.1)
    * activemodel (3.2.1)
    * activerecord (3.2.1)
    * activeresource (3.2.1)
    * activesupport (3.2.1)
    * arel (3.0.0)
    * builder (3.0.0)
    * bundler (1.0.21)
    * cocaine (0.2.1)
    * coffee-rails (3.2.2)
    * coffee-script (2.2.0)
    * coffee-script-source (1.2.0)
    * date_validator (0.6.3)
    * erubis (2.7.0)
    * execjs (1.3.0)
    * foreigner (1.1.1)
    * hike (1.2.1)
    * i18n (0.6.0)
    * journey (1.0.1)
    * jquery-rails (2.0.0)
    * json (1.6.5)
    * libv8 (3.3.10.4)
    * magic_encoding (0.0.2)
    * mail (2.4.1)
    * mime-types (1.17.2)
    * multi_json (1.0.4)
    * mysql2 (0.3.11)
    * paperclip (2.6.0 71d3f46)
    * polyglot (0.3.3)
    * rack (1.4.1)
    * rack-cache (1.1)
    * rack-ssl (1.3.2)
    * rack-test (0.6.1)
    * rails (3.2.1)
    * railties (3.2.1)
    * rake (0.9.2.2)
    * rdoc (3.12)
    * rmagick (2.12.0)
    * sass (3.1.15)
    * sass-rails (3.2.4)
    * sprockets (2.1.2)
    * stringex (1.3.0)
    * therubyracer (0.9.9)
    * thor (0.14.6)
    * tilt (1.3.3)
    * treetop (1.4.10)
    * tzinfo (0.3.31)
    * uglifier (1.2.3)
    * will_paginate (3.0.3)
  </code></pre>
  
  <p>It is interesting that in another project the same code works well. Gems in this project:</p>
  
  <pre><code>  * actionmailer (3.1.0.rc8)                                                                                                                                                          
    * actionpack (3.1.0.rc8)                                                                                                                                                            
    * activemodel (3.1.0.rc8)                                                                                                                                                           
    * activerecord (3.1.0.rc8)                                                                                                                                                          
    * activeresource (3.1.0.rc8)                                                                                                                                                        
    * activesupport (3.1.0.rc8)                                                                                                                                                         
    * addressable (2.2.6)                                                                                                                                                               
    * arel (2.2.1)                                                                                                                                                                      
    * authlogic (3.1.0)                                                                                                                                                                 
    * aws-s3 (0.6.2)
    * bcrypt-ruby (3.0.1)
    * builder (3.0.0)
    * bundler (1.0.21)
    * date_validator (0.6.3)
    * devise (1.5.3)
    * erubis (2.7.0)
    * execjs (1.3.0)
    * foreigner (1.1.1)
    * heroku (2.18.1)
    * hike (1.2.1)
    * i18n (0.6.0)
    * jquery-rails (1.0.19)
    * json (1.6.5)
    * launchy (2.0.5)
    * libv8 (3.3.10.4)
    * magic_encoding (0.0.2)
    * mail (2.3.0)
    * mime-types (1.17.2)
    * multi_json (1.0.4)
    * mysql2 (0.3.11)
    * nifty-generators (0.4.6)
    * orm_adapter (0.0.6)
    * paperclip (2.3.6)
    * polyglot (0.3.3)
    * rack (1.3.3)
    * rack-cache (1.0.3)
    * rack-mount (0.8.3)
    * rack-ssl (1.3.2)
    * rack-test (0.6.1)
    * rails (3.1.0.rc8)
    * rails-i18n (0.3.0)
    * rails3-jquery-autocomplete (1.0.5)
    * railties (3.1.0.rc8)
    * rake (0.9.2.2)
    * rdoc (3.12)
    * rest-client (1.6.7)
    * rmagick (2.12.0)
    * rubyzip (0.9.5)
    * sprockets (2.0.3)
    * stringex (1.3.0)
    * term-ansicolor (1.0.7)
    * therubyracer (0.9.9)
    * thor (0.14.6)
    * tilt (1.3.3)
    * treetop (1.4.10)
    * tzinfo (0.3.31)
    * uglifier (1.2.2)
    * warden (1.1.0)
    * will_paginate (3.0.2)
    * xml-simple (1.1.1)
  </code></pre>
  
  <p>I added a gem 'aws-sdk', but it did not help anything.
  Migration file:</p>
  
  <pre><code>class CreatePhotos &lt; ActiveRecord::Migration
    def self.up
      create_table :photos do |t|
        t.string  :source_type, :null =&gt; true
        t.integer :source_id, :null =&gt; true
        t.integer  :seq
        t.string   :img_file_name
        t.string   :img_content_type
        t.integer  :img_file_size
        t.datetime :img_updated_at
  
        t.string  :title, :null =&gt; true
        t.text    :desc, :limit=&gt;400, :null=&gt;true
  
  
        t.string :meta_title, :default =&gt; nil
        t.string :meta_desc, :default =&gt; nil
        t.string :meta_keywords, :default =&gt; nil
        t.timestamps
      end
  
      add_index(:photos, :source_id)
      add_index(:photos, :source_type)
      add_index(:photos, :seq)
    end
  
    def self.down
      drop_table :photos
    end
  end
  </code></pre>
  
  <p>I have no ideas to solve this problem. Thanks for any help</p>