1

我有一个模型用户:

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

结尾

4

2 回答 2

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

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

于 2013-05-29T14:58:27.557 回答
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

do这样做是使用循环内提供的相关信息创建 20 个用户。

于 2013-05-29T15:05:45.603 回答