NoMethodError(未定义的方法`new'代表nil:NilClass):当尝试创建Photo对象时

时间:2012-02-10 18:48:08

标签: paperclip nomethoderror ruby-on-rails-3.2

当我尝试将使用POST方法发送的数据分配给对象时,我收到以下错误。

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"=>"✓", "authenticity_token"=>"yeFZOu6sG3tyNqT4uBb5Xq0fp+1yTZ2wxn0WFPvGhPw=", "photo"=>{"source_type"=>"post", "source_id"=>"5", "cropping"=>"1", "img"=>#<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=#<File:/tmp/RackMultipart20120210-27534-1yyq97g>>}}
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)

在控制器中:

# -*- encoding : utf-8 -*-
class Backend::PhotosController < Backend::BaseController

  def create
    @photo = Photo.new(params[:photo])

    if @photo.save
      render :json => { :pic_path => @photo.img.url.to_s , :name => @photo.img.instance.attributes["img_file_name"] }, :content_type => 'text/html'
    else
      #todo handle error
      render :json => { :result => 'error'}, :content_type => 'text/html'
    end
  end

模特:

    # -*- encoding : utf-8 -*-

class Photo < ActiveRecord::Base
  scope :previous, lambda {|p| where("id < ? ", p.id).order(" id desc ").limit(1) }
  scope :next, lambda {|p| where("id > ? ", 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 => :cropping?

  has_attached_file :img, :styles => {
    :thumb =>  { :geometry => '130x130#', :quality => 60, :format => 'JPG', :processors => [:cropper]},
    :normal =>  { :geometry => '520x520>', :quality => 90, :format => 'JPG'},
    :big =>  { :geometry => '800x600>', :quality => 90, :format => 'JPG'}
  }

  validates_attachment_presence :img
  validates_attachment_size :img, :less_than => 5.megabytes
  validates_attachment_content_type :img, :content_type => [ 'image/jpeg', 'image/png', 'image/pjpeg' ]

在路线中:

  namespace :backend do
      root :to => "dashboards#show"  

      match "/set_per_page_admin" => "sessions#set_per_page_admin"
      match "/posts/:post_id/photos/:id/move_up" => "photos#move_up"
      match "/posts/:post_id/photos/:id/move_down" => "photos#move_down"
      match "/photos/reset_seq" => "photos#reset_seq"

      match "/photos/new/:source_type/:source_id" => "photos#new"
      resources :photos do
        member do
          get :crop
          get :crop_existing_photo
        end
      end


      scope "/remote" do
        match "/show_pic" => "photos#show_pic"
        match "/show_crop" => "photos#show_crop"
      end

      resources :posts
  end

已安装的宝石:

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)

有趣的是,在另一个项目中,相同的代码运行良好。这个项目中的宝石:

  * 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)

我添加了一个宝石'aws-sdk',但它没有任何帮助。 迁移文件:

class CreatePhotos < ActiveRecord::Migration
  def self.up
    create_table :photos do |t|
      t.string  :source_type, :null => true
      t.integer :source_id, :null => 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 => true
      t.text    :desc, :limit=>400, :null=>true


      t.string :meta_title, :default => nil
      t.string :meta_desc, :default => nil
      t.string :meta_keywords, :default => 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

我没有想法解决这个问题。谢谢你的帮助

0 个答案:

没有答案