2017-06-14 80 views
0

我剛剛按照文檔在配置文件中添加新字段,但似乎並不奏效。加班我得到Unpermitted parameters: firstname, lastname, password_confirmation無法在設計上添加新字段 - Ruby on Rails

這是我的用戶模型:

class User < ApplicationRecord 
    devise :database_authenticatable, :registerable, :recoverable, rememberable, :trackable, :validatable 
    has_many :issues 
    has_many :comments 
    has_many :likes 

    # Validations 
    validates :email, presence: true 
    validates :password, presence: true 

    # Omniauth 
    devise :omniauthable, omniauth_providers: [:facebook] 
    # Omniauth Facebook # Omniauth Facebook 
    def self.from_facebook(auth) 
     where(facebook_id: auth.uid).first_or_create do |user| 
     # user.image = auth.info.image 
     user.email = auth.info.email 
     user.firstname = auth.info.first_name 
     user.lastname = auth.info.last_name 
     user.country = auth.info.location 
     user.region = "" 
     user.password = Devise.friendly_token[0, 20] 
     # user.skip_confirmation! 
    end 
end 

## Paperclip avatar 
has_attached_file :avatar, styles: { 
    big: "1200x1200>", 
    medium: "300x300>", 
    thumb: "100x100>" 
}, default_url: "missing.png" 
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/ 

end 

這是我的應用程序控制器,它包含了新的色器件領域的配置:(first_namelast_name

class ApplicationController < ActionController::Base 
    protect_from_forgery with: :exception 

    # Devise parameters configuration 
    before_action :configure_devise_parameters, if: :devise_controller? 
    def configure_devise_parameters 
     devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit(:email, :avatar, :firstname, :lastname, :region, :country, :bio, :password, :password_confirmation, :current_password) } 
     devise_parameter_sanitizer.permit(:account_update) { |u| u.permit(:email, :avatar, :firstname, :lastname, :region, :country, :bio, :password, :password_confirmation, :current_password) } 
    end 
end 

回答

1

我添加了兩個新領域)User模型。然後我overrided色器件的RegistrationsController通過創建一個registrations_controller.rb這樣

class RegistrationsController < Devise::RegistrationsController 

    private 

    def sign_up_params 
    params.require(:user).permit(:first_name, :last_name, :email, 
    :password, :password_confirmation) 
    end 

    def account_update_params 
    params.require(:user).permit(:first_name, :last_name, :email, 
    :password, :password_confirmation, :current_password) 
    end 

並更新了色器件航線routes.rb文件中像

devise_for :users, :controllers => { registrations: 'registrations' } 

它爲我工作!

+0

所以

devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks', registrations: "users/registrations" } 

,我嘗試刪除我的應用控制方法(和地雷代替,當然你的領域),並沒有刪除它。它不工作了...... –

+0

因此你刪除了'configure_devise_parameters'方法並且像上面那樣定義'registrations_controller.rb' ...對嗎?你能告訴我這個錯誤嗎?並添加遷移的新領域? –

+0

當然,錯誤保持不變:'不允許的參數:名字,姓氏,password_confirmation'和添加遷移的新字段是http://chopapp.com/#3etzk654 –

0

創建一個用戶目錄

mkdir app/controllers/users 

創建一個名爲registrations_controller.rb

# app/controller/users/registrations_controller.rb 
class Users::RegistrationsController < Devise::RegistrationsController 
before_action :configure_sign_up_params, only: [:create] 
before_action :configure_account_update_params, only: [:update] 


protected 

def configure_sign_up_params 
    devise_parameter_sanitizer.permit(:sign_up, keys: [:firstname, :lastname, :email, :avatar, :password, :password_confirmation]) 
end 

def configure_account_update_params 
    devise_parameter_sanitizer.permit(:account_update, keys: [:firstname,:lastname,:email, :avatar, :password, :password_confirmation]) 
end 
end 

控制器omniauth_callbacks_controller.rb

# app/controller/users/omniauth_callbacks_controller.rb 
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController 
def facebook 
    @user = User.from_omniauth(request.env["omniauth.auth"]) 

    if @user.persisted? 
    sign_in_and_redirect @user, :event => :authentication 
    set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format? 
    else 
    session["devise.facebook_data"] = request.env["omniauth.auth"] 
    redirect_to new_user_registration_url 
    end 
end 
end 

的routes.rbuser.rb

def self.from_omniauth(auth) 
where(provider: auth.provider, uid: auth.uid).first_or_create do |user| 
    user.firstname = auth.info.first_name 
    user.lastname = auth.info.last_name 
    user.email = auth.info.email 
    user.avatar = process_url(auth.info.image + '?type=large') 
    user.password = Devise.friendly_token[0,20] 
end 
end 

def self.process_url(uri) 
avatar_url = URI.parse(uri) 
avatar_url.scheme = 'https' 
avatar_url.to_s 
end 
+0

我剛剛試過,它仍然不起作用,我得到了同樣的錯誤:'不允許的參數:名字,姓氏,密碼確認' –

+0

你可以在這裏看到日誌:http://chopapp.com/#tf4mercx –