提前为超长信息道歉,只是试图包含所有信息......
我有一个使用Devise的应用程序,我使用rakefile为用户提供数据库种子。这最近起作用,但我不能告诉我生命中最后几组变化发生了什么,它可能只需要一双新鲜的眼睛。
这里有一声巨响:
这是用户架构:
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "job_title"
t.string "first_name"
t.string "last_name"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.integer "role"
t.integer "company_id"
t.string "company_type"
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
结束
模型的相关部分......
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Before actions
before_save { self.email = email.downcase }
# Validation constants
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
# Validation filters
validates :email, presence: true, format: {with: VALID_EMAIL_REGEX}, uniqueness: {case_sensitive: false}
validates :role, presence: true
validates :company, presence: true
# Relationships
belongs_to :company, polymorphic: true
end
和控制器的东西:
class UsersController < ApplicationController
# Authenticate user
before_filter :authenticate_user!
def create
User.create(user_params)
end
private
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :encrypted_password, :job_title, :role, :company)
end
以及更多......
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
# Modules to include
include SessionsHelper
# Before filters
before_action :configure_permitted_parameters, if: :devise_controller?
# Ensure authorization is setup in every action in the application
# check_authorization
# Devise after sign in redirect to profile page
def after_sign_in_path_for(resource)
"/home?login=true"
end
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :first_name
devise_parameter_sanitizer.for(:sign_up) << :last_name
devise_parameter_sanitizer.for(:sign_up) << :job_title
devise_parameter_sanitizer.for(:sign_up) << :role
devise_parameter_sanitizer.for(:sign_up) << :company_id
devise_parameter_sanitizer.for(:sign_up) << :company_type
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:first_name, :last_name, :job_title, :current_password) }
end
end
最后,从我的rake文件:
Vendor.all.each { |vendor|
domain = EmailDomain.where(company: vendor).first.domain
5.times do |n|
first_name = Faker::Name.first_name
last_name = Faker::Name.last_name
email = "example-#{n+1}@#{domain}"
password = "password"
User.create!(first_name: first_name,
last_name: last_name,
email: email,
password: password,
password_confirmation: password,
role: User::VENDOR_USER,
company: vendor,
job_title: "Sales Manager")
end
}
错误:
rake aborted!
ActiveRecord::UnknownAttributeError: unknown attribute: role
/app/vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.5/lib/active_record/attribute_assignment.rb:50:in `rescue in _assign_attribute'
/app/vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.5/lib/active_record/attribute_assignment.rb:45:in `_assign_attribute'
/app/vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.5/lib/active_record/attribute_assignment.rb:32:in `block in assign_attributes'
/app/vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.5/lib/active_record/attribute_assignment.rb:26:in `each'
/app/vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.5/lib/active_record/attribute_assignment.rb:26:in `assign_attributes'
/app/vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.5/lib/active_record/core.rb:455:in `init_attributes'
/app/vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.5/lib/active_record/core.rb:198:in `initialize'
/app/vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.5/lib/active_record/inheritance.rb:30:in `new'
/app/vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.5/lib/active_record/inheritance.rb:30:in `new'
/app/vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.5/lib/active_record/validations.rb:39:in `create!'
/app/lib/tasks/demo_data.rake:73:in `block (2 levels) in demo_create_vendors_and_users'
/app/lib/tasks/demo_data.rake:68:in `times'
/app/lib/tasks/demo_data.rake:68:in `block in demo_create_vendors_and_users'
/app/vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.5/lib/active_record/relation/delegation.rb:46:in `each'
/app/vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.5/lib/active_record/relation/delegation.rb:46:in `each'
/app/lib/tasks/demo_data.rake:64:in `demo_create_vendors_and_users'
/app/lib/tasks/demo_data.rake:5:in `block (2 levels) in <top (required)>'
NoMethodError: undefined method `role=' for #<User:0x007fcc2fb90ef8>
/app/vendor/bundle/ruby/2.1.0/gems/activemodel-4.1.5/lib/active_model/attribute_methods.rb:435:in `method_missing'
/app/vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.5/lib/active_record/attribute_methods.rb:208:in `method_missing'
/app/vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.5/lib/active_record/attribute_assignment.rb:45:in `public_send'
/app/vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.5/lib/active_record/attribute_assignment.rb:45:in `_assign_attribute'
/app/vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.5/lib/active_record/attribute_assignment.rb:32:in `block in assign_attributes'
/app/vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.5/lib/active_record/attribute_assignment.rb:26:in `each'
/app/vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.5/lib/active_record/attribute_assignment.rb:26:in `assign_attributes'
/app/vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.5/lib/active_record/core.rb:455:in `init_attributes'
/app/vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.5/lib/active_record/core.rb:198:in `initialize'
/app/vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.5/lib/active_record/inheritance.rb:30:in `new'
/app/vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.5/lib/active_record/inheritance.rb:30:in `new'
/app/vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.5/lib/active_record/validations.rb:39:in `create!'
/app/lib/tasks/demo_data.rake:73:in `block (2 levels) in demo_create_vendors_and_users'
/app/lib/tasks/demo_data.rake:68:in `times'
/app/lib/tasks/demo_data.rake:68:in `block in demo_create_vendors_and_users'
/app/vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.5/lib/active_record/relation/delegation.rb:46:in `each'
/app/vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.5/lib/active_record/relation/delegation.rb:46:in `each'
/app/lib/tasks/demo_data.rake:64:in `demo_create_vendors_and_users'
/app/lib/tasks/demo_data.rake:5:in `block (2 levels) in <top (required)>'
Tasks: TOP => demo:add_data
(See full trace by running task with --trace)
它不仅仅是角色,如果我重新排列创建中参数的顺序,它会将错误提供给job_title。
最后一个注意事项,在最近的变化中,许多宝石的过期升级,包括从3.2.2到3.3.0的Devise ......看起来这个项目已经不再适用了。最近做出了强有力的参数更新,试图让它发挥作用。