Update user attributes without changing password in rails_admin - ruby-on-rails-3

In my app, I use devise to generate User model, add some attributes like job, role,... to this.
In rails_admin page, when edit an user, I only show fields: email, job, role, idcard (no include password and password_confirmation fields)
When I click Save, it show error because the password is shorter than 6 characters.
In the console, if I try
User.last.update_attributes(:role => "admin", "idcard" => "1233131")
It'll get the same error!
How can I fix this? Because the rails_admin (I think and be sure) didn't use update in registrations_controller to update user!

oh, yeah! I just fix my own problem by delete two validate of user model
validates :password, :length => { :minimum => 6 }
validates :password, :confirmation => true
and when I type
User.first.update_attributes(:email => "abc#yahoo.com")
It worked!!!

Related

Making select box a required field in some situations

I have a form which has a select box with categories. If a particular category is selected, another select box/dropdown is displayed to the user. I need this second dropdown to be a required field when this category is selected.
I cannot add validates :course, :presence => {:message => 'Course cannot be blank.'} to the model because this field is not always required, I need some other way to make it required only when certain category is selected in the first dropbox.
Thanks for your help
You could try using the if argument to validates, like this:
validates :course, :presence => {:message => "Course cannot be blink."}, :if => Proc.new { |u| u.first_dropdown_value == 'value_that_you_validate_courses_for' }
For more information, check out the Rails documentation on conditional validation.

Two forms conflict

In update user form, I want to split password from rest of the form.
In user model I validate it.
I want to have
form 1
User infos fields
form 2
password and confirm fields.
in php I would do something like if post password is empty, ignore it.
is there something like
if :password
do validation
end
I need the model part, I have the rest.
just need an idea how to split validation in model or how to achieve this.
I hope i did understand you
class User << AR
validates :password,
:lenght => {:between => 3..20},
:confirmation => true,
:if => Proc.new { self.password }
end

Validation on missing attributes on update_attributes

The question is very simple, does update_attributes validates every possible validation of the model, even if I don't want to update some of the attributes?
I have a edit view, where the user might change his password, but only if he passes it, i.e., if it's blank the password would not change.
So I do the following in the controller:
def update
params[resource_name].delete(:password) if params[resource_name][:password].blank?
params[resource_name].delete(:password_confirmation) if params[resource_name][:password_confirmation].blank?
params[resource_name].delete(:current_password) if params[resource_name][:current_password].blank?
if resource.update_attributes(params[resource_name])
...
end
end
I defined the following validation on the model:
validates :password,
:length => { :within => 6..40 }
So whenever i use call the update I get an error saying that the password is too short
Ps.: I'm using Devise to deal it this.
EDIT: Do any of you know if Devise already have any validation on the password? Cause, I removed the validation and it worked in the right way, but if I put a short password it still show a validation, saying it's too short.
Yes, Devise does provide validations on password if you've passed :validatable to the devise method in your model. The default configuration for password length is 6..128. You can override this in config/initializers/devise.rb (around line 101).
You can remove :validatable from your model and write your own validations if you prefer. If you do this and don't want the validation to run if the password isn't passed to update_attributes, then do something like this:
validates :password, :presence => true, :if => lambda { new_record? || !password.nil? }

How can I update only a single field of a user profile in Rails 3?

I'm adapting the authentication system from Rails 3 Tutorial by Michael Hartl to better learn about rails before (most likely) switching to a system like authlogic or devise. My authentication system works great, and I have a user model that now includes fields that build a user profile. For example, users can write a blurb about themselves such as "tennis player, rock climber".
What I'd like to do is to be able to have a simple form that updates only that specific field ("blurb"). I've set this up on a user profile page where the user clicks a link called "edit" and this causes a partial to render showing a form with a field to update a user blurb as such:
in _edit_blurb.html.erb:
<div class = "editforms">
<%= form_for current_user do |f| %>
<div class="field">
<%= f.text_field :blurb, :class => "inputform round", :placeholder => "blurb" %>
</div>
<div class="actions">
<%= f.submit "Update", :class => "submit_button round mini_button" %> <br>
</div>
<% end %>
</div>
This form submits to an update action in the users controller, and for the most part updates the user blurb and nothing else. However, one glitch is that this update action submits a nil password (as there is no password field in the form) - leading to a change in the user password. This is problematic for obvious reasons. Originally, I added a line :if => lamdba {new_record? || !password.nil? } to the password validation to allow the update from the edit_blurb partial to update without needing a password. But, as you can see this allows for a nil password to be saved.
validates :password, :presence => true,
:if => lambda{ new_record? || !password.nil? },
:confirmation => true,
:length => { :within => 6..40 }
How can I alter either the form or the validation (or both) so that a user can update a blurb without updating/inputting his password (and fixing the reset-to-nil password update problem)?
I think I've provided the relevant code, but will add more if it will help! Thanks everyone!
what you'll want to do is prevent the user model from saving the password when the password value is nil. I don't know how your user model is set up, but if you've been following MH's railstutorial, the password should be saved by a method called:
before_save :encrypt_password
in User.rb. To fix this, add:
before_save :encrypt_password, :if => lambda { !password.nil? }
This should work.

"Email is already taken" error when user logs in with client_side_validations and devise

I am trying to use both client_side_validations and devise in my Rails app, and get a weird bug in the log in form.
It's says that the email has already been taken and doesn't let the user to log in :)
Email is used as the login.
This is only when I set the :validate => true.
When I turn off the setting :validate => true, the log in is performed successfully.
What might cause this problem?
I got the answer from the author of the client_side_validations:
For login form you don't want to have the uniqueness validator. So you'll need to do something like:
f.text_field :email, :validate => { :uniqueness => false }
From what I've understood this is the workaround because of the conditional validations in devise.