我正在尝试将设计添加到Rails 4应用程序,并且我想在注册中添加新字段并删除一些现有字段。我已将路线更改为
devise_for :users, :controllers => {:registrations => 'registrations'}
我在我的内容中包含了以下内容 application_controller.rb
class ApplicationController < ActionController::Base
before_filter :configure_permitted_parameters, if: :devise_controller?
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :password) }
end
并且我的模型文件(user.rb
)中包含以下内容
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable
attr_accessor :username
end
和我的registration_controller.rb
包含
class RegistrationsController < Devise::RegistrationsController
skip_before_filter :require_no_authentication, :only => [ :new, :create, :cancel ]
prepend_before_filter :authenticate_scope!, :only => [:edit, :update, :destroy]
# GET /resource/sign_up
def new
super
end
# POST /users
def create
build_resource(sign_up_params)
respond_to do |format|
if resource.save
format.html { redirect_to profile_update_path, notice: 'User was successfully updated.' }
else
format.html { render action: "new" }
end
end
end
protected
def sign_up_params
devise_parameter_sanitizer.sanitize(:sign_up)
end
end
结果是
Started POST "/users" for 127.0.0.1 at 2014-03-05 20:11:43 +0530
Processing by RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"/Tu/QaH1gQgr73uND+fYcLzwer4yhaserghjNQxqazp=", "user"=>{"username"=>"testname", "password"=>"[FILTERED]"}, "commit"=>"Sign up"}
(0.2ms) BEGIN
SQL (0.4ms) INSERT INTO "users" ("created_at", "encrypted_password", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["created_at", Wed, 05 Mar 2014 14:41:43 UTC +00:00], ["encrypted_password", "$2a$10$9dLwOBN4qEc3Vgv8NiMVlOaOG.j4jbKNIEg1RPZPdohZYZsZQBY.."], ["updated_at", Wed, 05 Mar 2014 14:41:43 UTC +00:00]]
(39.4ms) COMMIT
Redirected to http://localhost:3000/profile
Completed 302 Found in 114ms (ActiveRecord: 40.0ms)
它不会将用户名保存在表中。请告诉我我哪里做错了。提前致谢!
更新
以下是我在表格中的字段列表
id | email | encrypted_password | reset_password_token | reset_password_sent_at | remember_created_at | sign_in_count | current_sign_in_at | last_sign_in_at | current_sign_in_ip | last_sign_in_ip | created_at | updated_at | username | first_name | last_name |
数据库的用户表中是否有一个用户名列?考虑到您已经声明attr_accessor :username
了,我想不会-ActiveRecord模型应该自动为其数据库字段创建访问器。因此,我的猜测是用户名未保存在数据库中,因为数据库中没有用户名字段。添加用户模型时是否进行了迁移?
如果有用户名字段,请尝试删除attr_accessor :username
。您可能正在覆盖内置的ActiveRecord getter / setter方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句