为什么我得到“NoMethodError Exception:undefined method`pams'for main:Object with rake db:seed”?

时间:2013-05-29 14:23:18

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 rake authlogic

我有一个模型用户:

class User < ActiveRecord::Base
   before_update :randomize_file_name

  attr_accessible :first_name, :last_name, :birth_date, :gender,:address,:city,:country,:landline,:mobile, :email, :password, :password_confirmation,:login,:terms_and_policy,:remember_token,:avatar, :readonly,:look
  attr_accessor :terms_and_policy, :remember_me, :readonly, :password_confirmation

seeds.rb

login = Faker::Name.first_name
first_name = Faker::Name.first_name
last_name = Faker::Name.last_name
email = Faker::Internet.email

user = User.create!(login: login, first_name: first_name, last_name: last_name, email: email, address: "indore", city: "indore", country: "india", mobile:"1234567890", gender: "Male", password: "111111",password_confirmation: "111111")
puts "#{user} is created #{login}"

当我运行rake db:create时,我收到错误:

NoMethodError Exception: undefined method `params' for main:Object

创建password_confirmation时我不使用user,因为它会引发验证错误。

当我追踪它时:

rake db:seed --trace
** Invoke db:seed (first_time)
** Execute db:seed
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:abort_if_pending_migrations
rake aborted!
undefined method `params' for main:Object

当我看到authlogic被定义时,我使用了params gem:

module Authlogic
module ControllerAdapters # :nodoc:
# Allows you to use Authlogic in any framework you want, not just rails. See the RailsAdapter or MerbAdapter
# for an example of how to adapt Authlogic to work with your framework.
class AbstractAdapter
  attr_accessor :controller

  def initialize(controller)
    self.controller = controller
  end

  def authenticate_with_http_basic(&block)
    @auth = Rack::Auth::Basic::Request.new(controller.request.env)
    if @auth.provided? and @auth.basic?
      block.call(*@auth.credentials)
    else
      false
    end
  end

  def cookies
    controller.cookies
  end

  def cookie_domain
    raise NotImplementedError.new("The cookie_domain method has not been implemented by the controller adapter")
  end

  def params
    controller.params
  end

  def request
    controller.request
  end

  def request_content_type
    request.content_type
  end

  def session
    controller.session
  end

  def responds_to_single_access_allowed?
    controller.respond_to?(:single_access_allowed?, true)
  end

  def single_access_allowed?
    controller.send(:single_access_allowed?)
  end

  def responds_to_last_request_update_allowed?
    controller.respond_to?(:last_request_update_allowed?, true)
  end

  def last_request_update_allowed?
    controller.send(:last_request_update_allowed?)
  end

  private
    def method_missing(id, *args, &block)
      controller.send(id, *args, &block)
    end
end

2 个答案:

答案 0 :(得分:0)

此错误:

undefined method `params' for main:Object

可能表示某些内容正在类或模块定义之外调用params。当代码在类或模块定义之外执行时,main:Object通常为self,例如

$ irb
1.9.3p392 :001 > self.class
 => Object 
1.9.3p392 :002 > self.inspect
 => "main"

由于问题发生在Rake任务中,请尝试在Rakefile的顶部添加以下内容:

require 'tracer'
Tracer.on

然后运行:

rake db:seed

你可能会看到发生了什么。

答案 1 :(得分:0)

好的,我注意到了一些东西,或者只是我,但你有:

login = Faker::Name.first_name
first_name = Faker::Name.first_name
last_name = Faker::Name.last_name
email = Faker::Internet.email

但是当你尝试创建它时:

user = User.create!( login: login, 
                 first_name: first_name, 
                 last_name: last_name, 
                 email: email, 
                 address: "indore", 
                 city: "indore", 
                 country: "india", 
                 mobile:"1234567890", 
                 gender: "Male", 
                 password: "111111",
                 password_confirmation: "111111")

create区域内,您不应该login: first_name而不是login: login。无论如何这里是另一个例子,也许这会帮助你:

 20.times do |n|

      email = Faker::Internet.email
      until User.find_by_email( email ) === nil
        email = Faker::Internet.email
      end

      login = Faker::Name.first_name
      first_name = Faker::Name.first_name
      last_name = Faker::Name.last_name

      d = User.create!(:login => name,
                   :full_name => fullname,
                   :email => email,
                   :password => password,
                   :password_confirmation => password,
                    )        
end

这样做会创建20个用户,并在do循环内提供相关信息。