2016-09-26 25 views
1

我有以下的ApplicationController:设计不插入用户名到数据库

class ApplicationController < ActionController::Base 
    protect_from_forgery with: :exception 
    before_action :configure_permitted_parameters, if: :devise_controller? 
    before_filter :require_vote 

    protected 

    def configure_permitted_parameters 
    devise_parameter_sanitizer.permit(:sign_up, keys: [:username, :email]) 
    # devise_parameter_sanitizer.for(:sign_up) << :username 
    end 

    private 

    def require_vote 
    unless cookies[:voted] 
     redirect_to vote_path 
    end 
    end 
end 

而且我有以下的用户模型:

class User < ApplicationRecord 
    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
    :recoverable, :rememberable, :trackable, :validatable 
    has_many :pictures 
    attr_accessor :username 
    validates :username, presence: true 
end 

,当我注册时,在日志中我看到它实际上并没有在数据库中插入用户名,但它在参数中获取用户名。我究竟做错了什么?

+0

请参阅本答案http://stackoverflow.com/a/18567908/2681997 –

+0

@ABPrime我得到这个未定义的方法'为'为#你的意思是? fork – Krigga

+0

您正在使用哪个rails版本? –

回答

0
class ApplicationController < ActionController::Base 
    include CanCan::ControllerAdditions 
    # Prevent CSRF attacks by raising an exception. 
    # For APIs, you may want to use :null_session instead. 
    protect_from_forgery with: :exception 

    before_action :configure_devise_permitted_parameters, if: :devise_controller? 


    protected 

    def configure_devise_permitted_parameters 
    registration_params = [:name, :email, :phone, :skype, :site, :description, :image, :image_cache, :password, :password_confirmation, :surname, :patronymic] 

    if params[:action] == 'update' 
     devise_parameter_sanitizer.for(:account_update) do |u| 
     u.permit(registration_params << :current_password) 
     end 
    elsif params[:action] == 'create' 
     devise_parameter_sanitizer.for(:sign_up) do |u| 
     u.permit(registration_params) 
     end 
    end 
    end 
end 

试试吧。