W
W
WEB_champion2014-11-03 21:48:04
Ruby on Rails
WEB_champion, 2014-11-03 21:48:04

In Rails, if the model has validates :password, presence: true, then how can you remove this obligation elsewhere?

The matter is that I want to implement functions for users (user), that is, adding and editing. And here in the model class is present:

class User < ActiveRecord::Base
  validates :password, presence: true
end

And when adding a new user, entering a password is required.
There is another situation already when editing. The editing form provides fields for editing:
E-mail
Login
Password
New password
And the password here is empty. And if the user does not want to change the password, then he must leave the field empty, that is, as it is. But when this field is sent for update, the data is not saved, because the password field is required.
How can you solve the problem, more precisely, how to remove the obligation for an exceptional situation?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
anyd3v, 2014-11-03
@WEB_champion

There are at least 2 ways
1. validates_presence_of :password, :on => :create
2. object.save(:validate => false)
have you tried them? why don't they suit you?

A
Alexander Gonchurin, 2014-11-05
@imalexgo

Turn off validation when saving.
Or define its use only when creating an object, above anyd3v answered correctly.
to help

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question