Ruby on rails 为什么我会得到;NoMethodError异常:未定义的方法'params';对于带有rake db:seed“的main:Object;?
我有一个模型用户:Ruby on rails 为什么我会得到;NoMethodError异常:未定义的方法'params';对于带有rake db:seed“的main:Object;?,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-3.1,rake,authlogic,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
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
当我运行rake db:create
时,我得到一个错误:
NoMethodError Exception: undefined method `params' for main:Object
当我创建用户
时,我不使用密码确认
,因为它会引发验证错误
当我追踪它时:
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
当我看到params
被定义时,我使用了authlogic
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
结束此错误:
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
你可能会看到事情发生的地方。好吧,我注意到了什么,或者只是我,但你有:
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\u 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
循环中提供相关信息 rake db:create
应该按照您的模式创建数据库和表。rb
,而不是运行seed(tryrake db:seed
),您可以运行rake db:create--trace
并找到实际引发异常的行吗?把它贴在这里?顺便说一句,当您仅运行db:create
任务时,不会使用seed.rb
。但我想使用seed将管理数据插入表中。那么我该怎么做Tracer.on::NoMethodError Exception:Tracer:ClassSomethings的未定义方法“on”就错了,因为这个方法很长一段时间以来一直是Ruby的stdlib的一部分,我所说的对我来说很有用。这是在。Tracer.method(Tracer.instance\u methods[0]).source\u location
返回什么?
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