У меня есть модель пользователя:Почему я получаю «NoMethodError Exception: неопределенный метод« params »для main: Object with rake db: seed»?
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
был определен:
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
конец
«rake db: create» должен создать вашу базу данных и таблицы, следующие за вашим «schema.rb», а не запускать семя (попробуйте «rake db: seed») – MrYoshiji
Можете ли вы запустить 'rake db: create - -trace' и найти строку, которая фактически вызывает исключение? И опубликовать его здесь? Btw, 'seeds.rb' не используется, когда вы запускаете только задачу' db: create'. –
Но я хочу вставить данные администратора в таблицу, используя семя. Так как я могу это сделать –