1

Hi am working on sample app where I am using devise for authentication. I am adding following extra parameters while registration.

:first_name, :last_name, :mobile, :gender, :address

But I am getting following Unpermitted parameters: first_name, last_name, password_confirmation error while registering new user.

I refereed following links Add Custom Field/Column to Devise with Rails 4

http://www.jacopretorius.net/2014/03/adding-custom-fields-to-your-devise-user-model-in-rails-4.html

But it didn't worked. Here is not code set

user.rb

class User < ActiveRecord::Base

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  validates_confirmation_of :password, :only => :create
end

I also implemented same in application controller but it didn't worked so I created separate registration controller.

registration_controller.rb

class RegistrationsController < Devise::RegistrationsController

  before_filter :configure_permitted_parameters

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) do |u|      
      u.permit(:first_name, :last_name, :mobile, :gender, :address, :email, :password, :password_confirmation)
    end
    devise_parameter_sanitizer.for(:account_update) do |u|
      u.permit(:name,
      :email, :password, :password_confirmation, :current_password)
    end
  end
end

application_controller.rb

class ApplicationController < ActionController::Base

  protect_from_forgery with: :exception

  private
  def after_sign_in_path_for(resource)
    user_landing_page
  end

  def user_landing_page
    contact_us_path      
  end
end

devise/registration/new.html.haml

.container
  .row
    .col-sm-6.col-md-4.col-md-offset-4
      %h1.text-center.login-title Create New Account
      .account-wall
        %img.profile-img{:alt => "", :src => "https://lh5.googleusercontent.com/-b0-k99FZlyE/AAAAAAAAAAI/AAAAAAAAAAA/eu7opA4byxI/photo.jpg?sz=120"}
          = form_for(resource, as: resource_name, class: "form-signin input-medium", url: session_path(resource_name)) do |f|
            = f.text_field :first_name, class: "form-control", placeholder: "First Name", autofocus: true
            = f.text_field :last_name, class: "form-control", placeholder: "Last Name", autofocus: true
            = f.email_field :email, class: "form-control", placeholder: "Email", autofocus: true
            = f.password_field :password, class: "form-control", placeholder: "Password", autocomplete: "off"
            = f.password_field :password_confirmation, class: "form-control", placeholder: "Confirm Password", autocomplete: "off"
            = f.submit "Log in", class: "btn btn-lg btn-primary btn-block login-button"

development.log

Parameters: {"utf8"=>"✓", "authenticity_token"=>"pe7wBW3iWnn39p3nJAi8utbuECj+x8zX/pIxr/6sKbo=", "user"=>{"first_name"=>"first_name", "last_name"=>"last_name", "email"=>"admin@my.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Log in"}
Unpermitted parameters: first_name, last_name, password_confirmation
  Rendered devise/sessions/new.html.haml within layouts/application (6.6ms)
  Rendered layouts/_home_header.html.haml (1.2ms)
Completed 200 OK in 426ms (Views: 316.8ms | ActiveRecord: 0.0ms)

routes.rb

Rails.application.routes.draw do
  root :to => 'landing#index'  
  devise_for :users, :controllers => {:registrations => "registrations"}
  get '/about_us' => 'statics#about_us', as: :about_us
  get '/contact_us' => 'statics#contact_us', as: :contact_us
end

can any one suggest what I am missing. I am using Rails 4.1.4, ruby 2.1.2 and devise 3.4.1.

Community
  • 1
  • 1
user2622247
  • 1,059
  • 2
  • 15
  • 26

1 Answers1

3

As per my understanding, you are registering new user. Here is your mistake

= form_for(resource, as: resource_name, class: "form-signin input-medium", url: session_path(resource_name)) do |f|

you are using session_path that means you are going to "Log In" not for "Sign Up". So change it

= form_for(resource, as: resource_name, class: "form-signin input-medium", url: registration_path(resource_name)) do |f|
shrikant1712
  • 4,336
  • 1
  • 24
  • 42