scarsi
scarsi

Reputation: 141

Rails 4 and Devise: Devise not saving new information (First Name, Last Name, Profile Name)

I am using Rails 4 and Devise. I am trying to display a user's first name in a status posted on my App. I have done everything I can think of correctly (migrated database after adding user model) and the database is still not saving the three new fields listed in my title, Devise saves the things it came with (email + password) but not the new fields.

Here is my user.rb:

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

  # Setup accessible attributes for your model     

  attr_accessible :role_ids, :as => :admin
  attr_accessible :email, :password, :password_confirmation, :remember_me, 
                  :first_name, :last_name, :profile_name



end

My db migration:

class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_table(:users) do |t|

      t.string: :first_name;
      t.string: :last_name;
      t.string: :profile_name;

      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      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

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      t.timestamps
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end
end

Having done some research, I don't think attr_accessible is compatible with Rails 4 and Devise, is this correct? If so, how do I pass this new information into my Devise db? Does anyone know a good guide to do this?

Let me know if there is any more information that needs to be provided!

Thanks in advance

Upvotes: 5

Views: 3908

Answers (4)

scarsi
scarsi

Reputation: 141

Cheers for your help guys, here's how I got it to work:

I updated my Application Controller to this:

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

  before_filter :configure_permitted_parameters, if: :devise_controller?

  protected

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

end

And made sure to add user_id to the bottom of your status controller:

params.require(:status).permit(:user_id, :name, :content)

This will save the new fields in your db and allow it to be displayed in statuses.

Upvotes: 9

user1854802
user1854802

Reputation: 388

1- as noted above you need to use strong parameters

2- I am using Rails '4.0.4' and devise. A couple of weeks ago tried to update to Rails 4.1.0. I got some errors from use of devise so I wnet back to rails version 4.0.4

Pierre

Upvotes: 0

user377277
user377277

Reputation:

You need to place the following in your ApplicationController.

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

Upvotes: 1

Ismoh
Ismoh

Reputation: 1104

Take a look at the answer of this question:
User model won't update
If the ruby-on-rails-4 Tag is correct, you should keep in mind that

With rails 4 attr_accessible is no longer used, instead we now have strong params.

Upvotes: 0

Related Questions