domi91c
domi91c

Reputation: 2053

Can't add custom Devise field through view

I've been at this for days, and cannot figure out the problem. When I attempt to Sign Up, it appears to work, but when I check the rails console, it shows no username. I'm able to add one from there and it works fine, but using my Sign Up view it never works. Only the fields that were created when I installed Devise work. I've also added First and Last Name, but I only attempted to get Username working, so the code for names is incomplete. If I've left anything important out, let me know. I'd REALLY appreciate the help.

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

protect_from_forgery with: :exception


  protected


  def configure_devise_permitted_parameters
      devise_parameter_sanitizer.for(:sign_up) do |u|
      u.permit(:email, :username, :password, :password_confirmation)
  end
end

.

class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
       :recoverable, :rememberable, :trackable, :validatable

validates :username, :uniqueness => {:case_sensitive => false}

has_many :posts
has_many :offers
has_many :requests
has_one :profile

def name
    "#{first_name} #{last_name}"
end
protected

def self.find_for_database_authentication(warden_conditions)
    conditions = warden_conditions.dup
    login = conditions.delete(:login)
    where(conditions).where(["lower(username) = :value", { :value => login.downcase }]).first
end


end

.

<h2>Sign up</h2>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>

<div><%= f.label :email %>
<%= f.email_field :email, autofocus: true %></div>


<div><%= f.label :username %>
<%= f.text_field :username%><div>

<div><%= f.label :first_name %>
<%= f.text_field :first_name %><div>
...

. class UsersController < ApplicationController before_action :set_user, only: [:show, :edit, :update, :destroy]

def show_user
    @user = current_user
            render 'show'
  end

def index
    @user = User.all
end

def new
    @user = User.new
    render profiles_path

end
def create
    @user = User.new(user_params)
    if @user.save
        redirect_to posts_path, notice: 'User successfully added.'

    else
        render profiles_path

    end
end
def edit
end
def update
    if @user.update(user_params)
        redirect_to posts_path, notice: 'Updated user information successfully.'
    else
        render action: 'edit'
    end
end
private
def set_user
    @user = User.find(params[:id])
end
def user_params
    params.require(:user).permit(:name, :first_name, :last_name, :email, :username)
end
end

.

class RegistrationsController < Devise::RegistrationsController
    def update
    new_params = params.require(:user).permit(:email,
                                              :username, :current_password, :password,
                                              :password_confirmation)
    change_password = true
    if params[:user][:password].blank?
        params[:user].delete("password")
        params[:user].delete("password_confirmation")
        new_params = params.require(:user).permit(:email,
                                                  :username)
        change_password = false
    end
    @user = User.find(current_user.id)
    is_valid = false
    if change_password
        is_valid = @user.update_with_password(new_params)
    else
        @user.update_without_password(new_params)
    end
    if is_valid
        set_flash_message :notice, :updated
        sign_in @user, :bypass => true
        redirect_to after_update_path_for(@user)
    else
        render "edit"
    end end
end

Schema:

create_table "users", force: true do |t|
  t.string   "email",                  default: "", null: false
  t.string   "encrypted_password",     default: "", null: false
  t.string   "reset_password_token"
  t.datetime "reset_password_sent_at"
  t.datetime "remember_created_at"
  t.integer  "sign_in_count",          default: 0,  null: false
  t.datetime "current_sign_in_at"
  t.datetime "last_sign_in_at"
  t.string   "current_sign_in_ip"
  t.string   "last_sign_in_ip"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.string   "first_name"
  t.string   "last_name"
  t.string   "username"
end

   add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
   add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using:     :btree

Upvotes: 0

Views: 78

Answers (1)

PeaceDefener
PeaceDefener

Reputation: 638

Add this to your ApplicationController

class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) << :username
  end
end

Source: https://github.com/plataformatec/devise#strong-parameters

Upvotes: 2

Related Questions