Rails: Combining last name and first name in Rails model - ruby-on-rails

FINAL SOLUTION:
I have a Rails 3 app that uses Devise to handle authentication. In the signup form i have the following fields:
<p><%= f.label :first_name %><br />
<%= f.text_field :first_name %></p>
<p><%= f.label :last_name %><br />
<%= f.text_field :last_name %></p>
I need to capitalize first and last names and combine them in the User model in a database field called 'login' (e.g. Lastname, Firstname). Here is the complete user model:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :remember_me, :login
validates :first_name, :last_name, :email, :password, :password_confirmation, :presence => true
before_create :create_login
def create_login
self.login = "#{last_name.capitalize}, #{first_name.capitalize}"
end
end
Thanks.

I genuinely, honestly, and truly applaud your use of the power of Ruby, but since this is such a straightforward and static concatenation of two strings, I'd go with:
def create_login
login = "#{last_name.capitalize}, #{first_name.capitalize}"
end
As for the nil:NilClass issue, are you adding first_name and last_name columns to your users table in your associated migration?

before_create :create_login
validates :first_name, :presence => true
validates :last_name, :presence => true
def create_login
login = [last_name, first_name].map(&:capitalize).join(", ")
end
Short explanation
I think it is good to get first_name and last_name on registration: so we will ad validation to it. Also it is good idea to validate length and match it with some regexp.
Then, as far as login is creates only once, we will add before_create callback, which will be executed only when object is creating (not updating). before_create callback will be run only if validation is passed, so if first_name or last_name is blank - validation won't be passed and callback won't be executed till first_name and last_name is filled.
UPD
Ok, as far as you get your error:
def create_login
login = [last_name, first_name].compact.map(&:capitalize).join(", ")
end

Related

Two models with one controller using devise, problems with validations and error messages

I have two models in my app, one 'user' model with devise, one 'employee' model made by scaffolding.
I need a way to have the employee table populated as soon as a new user registers, both tables share some params, some are exclusive. The employee belongs to the user model, each user has one employee.
The view I use is the devise user registration form with nested attributes to allow for the employee params. Creation is handled by the user controller.
Problems that occur:
undefined method 'email' for empty password field until the first user is created
cannot make the error message for the exclusive 'name' parameter of the employee model show up in the same place as the error messages for the user model
employee-model:
class Employee < ApplicationRecord
audited
validates :email, presence: true, uniqueness: true
validates :name, presence: true
belongs_to :user, inverse_of: :employee, dependent: :destroy, optional: true
user model
class User < ApplicationRecord
audited
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable
has_one :employee, inverse_of: :user
accepts_nested_attributes_for :employee
validates_presence_of :password
validates :password, format: { with: /(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[["§#?!`´~#'._,;<>|°()"{}="#$%^&*+-]]).{8,}/}, if: -> {password.present?}
validates_presence_of :password_confirmation, if: -> {password.present? and password =~ /(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[["§#?!`´~#'._,;<>|°()"{}="#$%^&*+-]]).{8,}/}
validates_confirmation_of :password, if: -> {password.present? and password_confirmation.present? and password =~ /(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[["§#?!`´~#'._,;<>|°()"{}="#$%^&*+-]]).{8,}/}
validates_presence_of :email
validates :email, format: { with: /\A([^#[0-9]\s]+\.)+([^#[0-9]\s]+)#((thisapp+\.)+com)\z/i}, uniqueness: true, if: -> {email.present?}
end
user controller
class Users::RegistrationsController < Devise::RegistrationsController
# before_action :configure_sign_up_params, only: [:create]
# before_action :configure_account_update_params, only: [:update]
# GET /resource/sign_up
def new
super
end
def new_employee
#employee = Employee.new
end
# POST /resource
def create
#employee = Employee.new(employee_params)
super
#employee.email = User.last.email
#employee.user_id = User.last.id
#employee.created_by_id = User.last.id
#employee.save
end
user registration view
.card style='text-align:center;'
.card-body
h2.card-title style='text-align:center;' = t('.sign_up')
= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f|
= render "users/shared/error_messages", resource: resource
br
.field
= f.label :email, t('users.email_field')
br
= f.text_field :email, autofocus: true, autocomplete: "email"
.field
= f.label :password, t('users.password_field')
/- if #minimum_password_length
em
= t('devise.shared.minimum_password_length', count: #minimum_password_length)
br
= f.password_field :password, autocomplete: "new-password"
.field
= f.label :password_confirmation, t('users.password_confirmation_field')
br
= f.password_field :password_confirmation, autocomplete: "new-password"
= fields_for :employee do |e|
= e.label :name
= e.text_field :name
So, when I use #employee.email = User.last.email in the controller I get a 'unknown method 'email' error when not filling out the password field, unless I have a preexisting user, then I get my custom error messages for not filling out the email. I guess it is because I am looking for a last user who does not exist at this point. Could seed a user, but that seems hackish. Tried using #employee.email = User.last(params[:email]) which leads to the email being saved as some hash value, but at least I get my error messages. Is there a way to convert that hash to the real email address again?
The other issue is the validation of the name field. Validation is asked for in the employee model, and user model accepts nested attributes, but that does not seem to be enough.
I did
if params[:employee][:name].blank?
flash[:notice] = t('.noname')
which works insofar as that the form cannot be submitted without some value in the name field, but messes up my error messages. Shows a flash message where all other errors (no email/password/pw confirmation) are handled by the devise's shared error messages as non-flash messages:
- resource.errors.full_messages.each do |message|
li
= message
So having the blank name as a flash message would look inconsistent, and the spot for the flash message is already reserved.
Flash message is on top, 'following errors prevent..', actual error messages are below 'Registrieren', and that is also where the error message for blank name would need to be.
Any ideas on how to approach this or maybe a better solution than handling this stuff in the user controller?
class Users::RegistrationsController < Devise::RegistrationsController
# GET /resource/sign_up
def new
super
end
def new_employee
#employee = Employee.new
end
def sign_up_params
params.require(:user).permit(:email, :password, :password_confirmation, employee_attributes: %i[name])
end
This signup param will be used while creating a user from devise registration controller. Since we used nested attributes, passing the arguments along with the parent object will handle employee creation(https://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html)
For assigning email for employee
The one way is to merge the email from the user params.
def sign_up_params
sign_up_param = params.require(:user).permit(:email, :password, :password_confirmation,employee_attributes: %i[name])
sign_up_param[:employee_attributes].merge!(email: params[:user][:email])
sign_up_param
end
Or maybe you could assign the email of the employee from the user before the validation.
class Employee < ApplicationRecord
audited
validates :email, presence: true, uniqueness: true
validates :name, presence: true
belongs_to :user, inverse_of: :employee, dependent: :destroy, optional: true
# Callbacks
before_validation :set_email
# Methods
# Set email of the employee from the user.
def set_email
self.email = self.user.email
end

Custom fields in form_for

Trying to create a profile form in which a user can add information about their hobbies to their user profile. But I get the following error when trying to do so.
NoMethodError in Users#edit_profile
undefined method `hobbies' for #<User:0x007ff1a8a1f198>
Does anyone know what the problem could be, or a potential solution? I'm new to rails but I was under the impression that 'text_field' was a safe bet to make any custom input work. Would installing the strong parameters gem help this out at all?
edit_profile.html.erb
<h2>Tell us about yourself</h2>
<%= form_for #user do |f| %>
<%= f.label :first_name %><br />
<%= f.text_field :first_name, autofocus: true %>
<%= f.label :last_name %><br />
<%= f.text_field :last_name %>
<%= f.label :hobbies %><br />
<%= f.text_field :hobbies %>
<div><%= f.submit "Update" %></div>
<% end %>
user_controller.rb
class UsersController < ApplicationController
before_filter :authenticate_user!
def index
#users = User.all
end
def show
#user = User.find(params[:id])
end
def new
#user = User.new
end
def create
end
def edit
end
def update
#user = User.find(params[:id])
#user.update!(user_params)
redirect_to #user
end
def destroy
end
def edit_profile
#user = User.find(params[:id])
end
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :hobbies)
end
end
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
attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :remember_me, :hobbies
#validates :first_name, presence: true, length: { maximum: 50 }
#validates :last_name, presence: true, length: { maximum: 50 }
end
You don't mention it, but I will assume you're running Rails 4.x.
Rails 4.x introduced strong parameters, so in your controller you need to add a private method to set the allowed parameters and remove the attr_accessible from your model.
So in you case it will be:
def user_params
params.require(:first_name, :last_name).permit(:email, :password, :password_confirmation, :remember_me, :hobbies)
end
If you still have trouble to understand the concept, or came from a previous Rails version, take a look at this blog post.
Does anyone know what the problem could be, or a potential solution?
Sure - the problem is you don't have a hobbies attribute in your User model :)
Since you're new, I'll explain a bit about Rails after I answer the question. However, let me explain the value of creating the right attributes:
#app/models/user.rb
class User < ActiveRecord::Base
attr_accessor :hobbies
end
This is what you'll need to create a single attribute for your User model. The attr_accessor directive is a Ruby method which sets a getter and setter in your User model
This will give Rails the ability to populate the hobbies attribute of your Model. However, the data will not be persistent, as it will only be set on a per-instance basis in the model, meaning it will be lost when you refresh the page etc
Using the code above should get your form working, regardless of whether you're using Rails 3 or 4.
Models
Rails is famously an MVC framework - which means it has 3 core components - models (builds data from the database), controllers (configure data for the view) & views (displays the data from the controller & model).
When you load a Rails application, you're sending a request, which will be routes to a particular controller action. This will then call data from your database, allowing you to manipulate it in your view.
Models, therefore have to be populated from your database. They do this by taking the various attributes you have in your datatables, and creating a series of getters and setters for them. These give you the ability to access the data within the attributes, or set new ones.
Your error occurs because you don't have the relevant attribute set in your datatable. You'll need to create a migration to add the hobbies attribute to your User model:
> $ rails g migration AddHobbiesToUser hobbies:string
> $ rake db:migrate
The migration should create something like:
class AddPartNumberToProducts < ActiveRecord::Migration
def change
add_column :users, :hobbies, :string
end
end

Check validates_uniqueness_of in rails for two values in or condtion

i'm having problem in checking validates_uniqueness_of which has two condtion.
Class name is InviteGuest
class InviteGuest < ActiveRecord::Base
attr_accessible :invite_id, :email, :first_name, :last_name, :random_no
validates_presence_of :invite_id, :unless => :random_no?
validates_uniqueness_of :email, :scope => [:invite_id]
Here i'm checking invite_id or :random number for presence.
so here my :email feilds are entering first with unique :random_no .which works fine.and i;m validates_uniqueness_of :email, :scope => [:invite_id] but it's not allowing me to enter same :email for different :random_no. i have to put or condition. it's cheking for :invite_id but i would like to check it for :random_no if :invite_id is not present. but it's not allowing me to enter :email for different :random_no.
Any idea??
It sounds to like your scope is not set correctly. You should scope on random_no:
validates_uniqueness_of :email, scope: [:invite_id, :random_no]

Update user with devise polymorphic association

I experiencing an issue on the update of a polymorphic association.
Actually, I've several type of users such as Admin, Customer, etc...
But on the update of a customer (for example), it fails because devise ask for a password.
I've the User model which only have devise logic:
class User < ActiveRecord::Base
devise :database_authenticatable,
:registerable,
:recoverable,
:rememberable,
:trackable,
:validatable
belongs_to :role, polymorphic: true
end
customer.rb:
class Customer < ActiveRecord::Base
has_one :user, as: :role, dependent: :destroy
end
And on the controller side, customers_controller.rb:
def update
if #customer.update customer_params
redirect_to dashboard_path, flash: { success: t('validation.update', model: #customer.class.model_name.human.downcase) }
else
render 'edit'
end
end
private
def customer_params
params.require(:customer).permit(:firstname, :lastname, user_attributes: [:email, :password, :password_confirmation])
end
Here is my form view:
= simple_form_for #customer do |f|
.form-inputs
= f.fields_for :user do |u|
= u.input :email, required: true, autofocus: true
= u.input :password, autocomplete: 'off', hint: t('devise.registrations.edit.leave_blank_if_you_don_t_want_to_change_it'), required: false
= u.input :password_confirmation, required: false
= u.input :current_password, hint: t('devise.registrations.edit.we_need_your_current_password_to_confirm_your_changes'), required: true
= f.input :firstname
= f.input :lastname
I see that in the form you have added required: false for password and password_confirmation field.
The required attribute is a boolean attribute. When present, it
specifies that an input field must be filled out before submitting the
form.
BUT that is not going to restrict Devise from asking for password. By default, in Devise its mandatory which will performed every time you update a record.
If you want to update the record without providing password then follow the guidelines mentioned in Devise How To: Allow users to edit their account without providing a password

Rails 3 + devise + active_scaffold how make password not required

I have the model Agency based on Devise:
class Agency < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :name, :email, :phone, :address, :city, :state, :zip,
:notes, :is_admin, :password, :password_confirmation, :remember_me
protected
def password_required?
!persisted? || password.present? || password_confirmation.present?
end
end
Also I have the AgenciesController based on active_scaffold:
class Admin::AgenciesController < Admin::BaseController
before_filter :authorize_admin!
active_scaffold :agency do |conf|
actions.exclude :show
create.link.page = true
update.link.page = true
list.columns = [
:name, :email, :phone, :address, :city,
:state, :zip, :notes, :events, :is_admin
]
create.columns = [
:name, :email, :phone, :address, :city, :state, :zip,
:is_admin, :password, :password_confirmation
]
update.columns = [
:name, :email, :phone, :address, :city, :state, :zip,
:is_admin, :password, :password_confirmation
]
columns.add :password
columns.add :password_confirmation
columns[:password].form_ui = :password
columns[:password_confirmation].form_ui = :password
end
end
Then here is Agency update form:
I would like to provide an opportunity for user to omit filling in of password and password_confirmation fields. But if user fills in password field the password_confirmation field have to be required.
I almost solved my problem by password_required? method. But javascript required verification on client doesn't allow me solve my problem completely.
How Can I remove JS varification from client for password and password_confirmation fields?
Try:
validates_confirmation_of :password, :if => :password_required?
validates :password_confirmation, :presence => true, :if => '!password.nil?'
Note that the validation is only triggered if password_required? returns true, and password_required? will return false if the password_confirmation field is nil.
So in order to make password_confirmation field to be nil, you simply need to remove the password_confirmation field from the form and that way you will always get it nil thus bypassing the validations.
Alternate Solutions:
1) Skip validation of password field
2) Just save a random secure generated number in the password field ( Better because it's easy and also maintain the consistency ).
self.password = self.password_confirmation = SecureRandom.urlsafe_base64(n=6)
Also if you want that such fields should be identified differently as been left by the user then you can give unique passwords to them so later you impose the condition like:
if(self.password == "something unique" and self.password_confirmation == "something unique")
flash[:notice] = "The user has not password"
end
Then on UI level, you simply display with blank using the above condition.
#Saurabh Jain, thanks a lot for your advices.
But solution was so much simple.
I have found out that active_scaffold adds an required attribute for input tag of password field.
By another words a client gets an html
<input type="password" required="required" size="30" name="record[password_confirmation]" id="record_password_confirmation_4" class="password_confirmation-input text-input" autocomplete="off">
Exactly required attribute was my problem :) To remove it from html simply add line
columns[:password].required = false;
to active_scaffold configuration in Admin::AgenciesController (see it above)

Resources