我想添加設計到rails 4應用程序,我想在註冊中添加新字段並刪除一些現有字段。我改變了路線如何添加自定義字段以在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)
它不保存用戶名在表中。請告訴我我哪裏出錯了。提前致謝!
UPDATE
以下是場我有表
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 |
請參閱我的更新以獲取用戶表 – logesh
中的字段列表好的,請參閱上面的編輯。您可能需要刪除attr_accessor – gregates
謝謝。在刪除attar_accessor它的工作,並可能我知道什麼devise_parameter_sanitizer.for(:sign_up){| u |使用u.permit(:username,:password)}。如果我發送值的用戶名和密碼不同意它會接受? – logesh