nahtnam
nahtnam

Reputation: 2689

Devise: Capitalize First Name

I added a new field in devise called firstname, and I want it to be capitalized by devise during registration.

I first ran: rails generate migration add_username_to_users firstname:string

then

rake db:migrate

After that I added firstname to the configure_permitted_parameters in the application_controller.rb and updated the views. I basically used this but stripped out some unnecessary stuff.

I dont know where I should put the code for capitalizing the firstname and lastname (as well as some other validating). Any guidance would be appreciated. Thanks.

Upvotes: 1

Views: 951

Answers (5)

Arish Khan
Arish Khan

Reputation: 254

Best Solution Ever:

 class Role < ApplicationRecord
  before_save :capitalize_names
  def capitalize_names
    self.name.titlecase
  end
 end

Output will be:

'super admin'.titlecase
 Super Admin

Upvotes: 0

Richard Peck
Richard Peck

Reputation: 76784

before_create

Joe Kennedy's answer is correct - you should use the before_create ActiveRecord callback

The difference here is that Devise doesn't do anything with your actual data modelling - it basically just creates a series of controllers to handle the user registration & login processes

--

If you want to ensure certain attributes of your User model are saved in a particular style, you'll be best setting it in the model itself:

#app/models/user.rb
Class User < ActiveRecord::Base
   before_create :set_firstname

   private

   def set_firstname
      self.firstname.titeize
   end
end

This should allow you to set the attribute to have the first letters of each word capitalized

--

System

An alternative would be to look at your system

Why are you insisting the data be stored this way? It seems very inefficient to save all your data in the same for the sake of styling.

I would use the CSS text-transform function to do this:

#app/assets/stylesheets/application.css
.first_name { text-transform: capitalize; }

#app/views/users/show.html.erb
<%= content_tag :span, @user.firstname, class: "first_name" %>

Upvotes: 3

Joe Kennedy
Joe Kennedy

Reputation: 9443

I think you should put capitalization of first and last names in your User model. Every time a user is saved, you can capitalize the first and last name. In addition, all validation (or attribute pre-processing/sanitization) can be done at the model level as well.

class User < ActiveRecord::Base
  before_save :capitalize_names

  def capitalize_names
    self.firstname = firstname.camelcase
    self.lastname = lastname.camelcase
  end
end

Upvotes: 5

Mohamed El Mahallawy
Mohamed El Mahallawy

Reputation: 13862

def create
  User.create(email: params[:email], first_name: params[:first_name].capitalize)
end

Although I'd suggest you just output the capitalize in your views and not when saving.

Upvotes: -1

Kyle Bachan
Kyle Bachan

Reputation: 1107

This should probably go in the User Controller (or whichever controller inherits from the Devise Controller and creates the new user). In the create method, before you save the user to the database, add whatever attributes you want to it (i.e. capitalizing the first letter) and then save it.

Upvotes: -1

Related Questions