我想在用户模型中添加属性名称。我运行了迁移命令,将列添加到数据库中并且工作正常。将属性添加到用户本身也可以,但它不会保存在数据库中。
如何将属性“name”添加到RegistrationController的sign_up
和account_update
所需的参数中?
这是我的用户模型
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessor :name
end
我尝试在RegistrationController
class Users::RegistrationsController < Devise::RegistrationsController
def sign_up_params
params.require(:user).permit(:name,:email, :password, :password_confirmation)
end
def account_update_params
params.require(:user).permit(:name, :email, :password, :password_confirmation, :current_password)
end
end
在路线中我添加了一行
devise_for :users, controllers: { registrations: 'users/registrations' }
但是用户名仍未保存在数据库中。
答案 0 :(得分:2)
将此添加到class TabBarController: UITabBarController {
var viewControllerToSelect: UIViewController?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
delegate = self
}
func showLeavingAlert() {
let leavingAlert = UIAlertController(title: "Warning", message: "Do you want to save before you leave?", preferredStyle: .Alert)
let saveAction = UIAlertAction(title: "Yes", style: .Default) { (action) in
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(1 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
// switch viewcontroller after one second delay (faked save action)
self.performSwitch()
}
}
leavingAlert.addAction(saveAction)
let cancelAction = UIAlertAction(title: "No", style: .Cancel) { (action) in
// switch viewcontroller immediately
self.performSwitch()
}
leavingAlert.addAction(cancelAction)
presentViewController(leavingAlert, animated: true, completion: nil)
}
func performSwitch() {
if let viewControllerToSelect = viewControllerToSelect {
// switch viewcontroller immediately
selectedViewController = viewControllerToSelect
// reset reference
self.viewControllerToSelect = nil
}
}
}
extension TabBarController: UITabBarControllerDelegate {
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
if let navigationController = selectedViewController as? UINavigationController, _ = navigationController.visibleViewController as? FavoritesViewController {
// save a reference to the viewcontroller the user wants to switch to
viewControllerToSelect = viewController
// present the alert
showLeavingAlert()
// return false so that the tabs do not get switched immediately
return false
}
return true
}
}
以配置ApplicationController
signup
&amp; signin
参数。
account_update
并将def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name,:email, :password, :password_confirmation) }
devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :username, :email, :password, :remember_me) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:name, :username, :anonymous, :email, :password, :password_confirmation,:reset_password_token) }
end
添加到before_filter
,例如:
ApplicationController
答案 1 :(得分:0)
请检查Devise Parameter Sanitization
你可以试试这个:
class Users::RegistrationsController < Devise::RegistrationsController
def sign_up_params
devise_parameter_sanitizer.for(:sign_up).push(:name)
end
def account_update_params
devise_parameter_sanitizer.for(:account_update).push(:name, :email, :password, :password_confirmation, :current_password)
end
end