0

i am trying to add the fields 'first name', 'last name' and 'phone'. However, i always get this error when revisiting local host:

NoMethodError in Devise::RegistrationsController#new

undefined method `configure_permitted_parameters' for #<Devise::RegistrationsController:0x00000101fcd008>

This is what i did:

first, i created a registrations controller:

class RegistrationsController < Devise::RegistrationsController
    before_filter :configure_permitted_parameters, :only => [:create]

  protected



       def configure_permitted_parameters
            devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :phone, :email, :password, :password_confirmation) }
        end
end

then, i ran:

rails g migration AddFieldsToUsers first_name:string last_name:string phone:int

followed by:

rake db:migrate

in the terminal...

i then went and added

      t.string :first_name,
      t.string :last_name,
      t.integer :phone,

in the devise create users migration file.

i also tried replacing add_column to t.string in my AddFieldsToUsers migration file:

class AddFieldsToUsers < ActiveRecord::Migration
  def change
    t.string :users, :first_name, :string
    t.string :users, :last_name, :string
    t.integer :users, :phone, :int
  end
end

I would really appreciate it if someone could help figure this out thank you.

illywilly
  • 323
  • 1
  • 5
  • 18

3 Answers3

1

First - about migration:

class AddFieldsToUsers < ActiveRecord::Migration
  def change
    add_column :users, :first_name, :string
    add_column :users, :last_name, :string
    add_column :users, :phone, :integer
  end
end

$ rake db:migrate

Second - about permissions in rails4:

this helpfull https://stackoverflow.com/a/19793371/3563993 and this

application_contoller.rb

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

  ...

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :first_name, :last_name, :fone) }
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:email, :password, :password_confirmation, :current_password, :first_name, :last_name, :fone) 
  end
end
Community
  • 1
  • 1
shilovk
  • 11,718
  • 17
  • 75
  • 74
  • Havent gotten it yet, however a new error does occur. ('NoMethodError in Devise::Registrations#new') and this bit of code is highlighted: <%= f.text_field :fist_name, :placeholder => "First Name", class: "form-control" %> – illywilly Dec 24 '14 at 06:20
  • 1
    @Taimur K Naziri I think your table users not have first_name, do First step and run: rake db:migrate – shilovk Dec 25 '14 at 13:27
0

Put first_name,last_name, phone, after attr_accessible: in user class. If You are using rails 3

djadam
  • 649
  • 1
  • 4
  • 20
  • I am using Rails 4, there is no attr_accessible, it is replaced by strong_params – illywilly Dec 22 '14 at 07:20
  • Please look this link, i think it will helpful you..http://www.jacopretorius.net/2014/03/adding-custom-fields-to-your-devise-user-model-in-rails-4.html – djadam Dec 22 '14 at 07:22
0

You should add action configure_permitted_parameters in application controller as per devise documentation.

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

Your error directly indicates that you did not have configure_permitted_parameters method for new action.

Dipak Gupta
  • 7,321
  • 1
  • 20
  • 32