我正在使用 Rails 4 和 Devise。 我正在尝试在我的应用程序上发布的状态中显示用户的名字。 我已经正确地完成了我能想到的所有事情(添加用户模型后迁移了数据库)并且数据库仍然没有保存我标题中列出的三个新字段,Devise 保存了它附带的东西(电子邮件+密码)但没有保存新字段.
这是我的 user.rb:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible attributes for your model
attr_accessible :role_ids, :as => :admin
attr_accessible :email, :password, :password_confirmation, :remember_me,
:first_name, :last_name, :profile_name
end
我的数据库迁移:
class DeviseCreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
t.string: :first_name;
t.string: :last_name;
t.string: :profile_name;
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
## Trackable
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
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
t.timestamps
end
add_index :users, :email, unique: true
add_index :users, :reset_password_token, unique: true
# add_index :users, :confirmation_token, unique: true
# add_index :users, :unlock_token, unique: true
end
end
经过一些研究,我认为 attr_accessible 与 Rails 4 和 Devise 不兼容,这是正确的吗? 如果是这样,我如何将这些新信息传递到我的 Devise 数据库中? 有谁知道执行此操作的好指南?
如果需要提供更多信息,请告诉我!
提前致谢
最佳答案
为你们的帮助干杯,这是我如何让它工作的:
我将我的应用程序 Controller 更新为:
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
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name,:last_name,:profile_name,:email, :password) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:first_name,:last_name,:profile_name,:email, :password) }
end
end
并确保将 user_id 添加到状态 Controller 的底部:
params.require(:status).permit(:user_id, :name, :content)
这会将新字段保存在您的数据库中,并允许它显示在状态中。
关于ruby-on-rails - Rails 4 和 Devise : Devise not saving new information (First Name, 姓氏,个人资料名称),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23257562/