how to locale field Devise? - ruby-on-rails

I used gem Devise for authentication in my application . I want to change label text for below input
<%= f.input :remember_me, as: :boolean if devise_mapping.rememberable? %>
but I can not find this field for standard renaming either in the service files or in the devise locale
how to locale this field ?
presently
check-box "Remember me"
_
need
check-box "Запомнить меня"

en.yml
en:
activerecord:
attributes:
user:
email: Email
password: Password
remember_me: Remember me
create new file for any other locale and add translated text for that language
de.yml
de:
activerecord:
attributes:
user:
email: "email in de"
password: "Password in de"
remember_me: "Remember me in de"
You can read more about devise translation here
Here is the very useful site for translations - https://www.localeapp.com/projects/377/translations/5250293?in_locale=5291

Related

Rails 4.1 Devise 3.3 column users.password does not exist

I want to create a user manually through the console as such:
User.find_or_create_by(email: "user#mail.com", first_name: "Stan", last_name: "Smith", password: "password", password_confirmation: "password", confirmed_at: Time.now)
I have done this many times in past projects but this time it's not working. It is not picking up the Devise password model attribute so this is the error I get:
PG::UndefinedColumn: ERROR: column users.password does not exist
I have Rails 4.1.4 and Devise 3.3.0. Did something change in the latest versions?
Thanks.
Instead of User.find_or_create_by you should be using User.create.
Devise accepts a password and password confirmation on creation but the actual table only has a column called encrypted_password.
The "find" portion of User.find_or_create_by is looking for a column called "password" which doesn't exist.
Still if you want to use find_or_create_by, it accepts block:
User.find_or_create_by(email: "user#mail.com", first_name: "Stan", last_name: "Smith") do |user|
user.password = "password"
user.confirmed_at = Time.now
end

Translate devise labels in registration?

I want to translate my labels in registration form, but I can't see what is the schema to follow in my .es locale. It wont work with "registrations". Someone knows? I can't find a correct answer in all the internet. Thanks
.es locale
devise:
registrations:
name: "Nombre"
email: "Email"
password: "Contraseña"
password_confirmation: "Recordarme"
It's done via active record translations
es:
activerecord:
attributes:
user:
password: "Contraseña"
email: "Email"
password_confirmation: "Recordarme"

How can I change devise alert message for password_confirmation

I am trying to change the alert message from the standard "doesn't match Password" to "The passwords do not match" but I can't figure out how to do it. I've already tried adding the following to my devise.en.yml file:
activerecord:
errors:
models:
user:
attributes:
password:
confirmation: "hey you billy"
blank: "cannot be blank."
invalid: "this password be invalid yo"
but this does not work. The blank message works but confirmation does not. Does anyone know how to fix this?
Try setting 'confirmation' on the 'password_confirmation' attribute instead;
activerecord:
errors:
models:
user:
attributes:
password_confirmation:
confirmation: "The passwords do not match"

Modify Devise reset password error text

Using the Devise GEM, when a user password is reset they are allowed to set a new password. If the entered passwords do not match or if the password is too short, you get default messages:
•Password doesn't match confirmation
•Password is too short (minimum is 8 characters)
How/where can I change the text of these error messages?
Add this to your config/locals/en.yml and change it to what you want
en:
activerecord:
errors:
models:
user:
attributes:
password:
confirmation: "Password does not match"
too_short: "is too short (minimum is %{count} characters)"
attributes:
user:
password: "Password"

password_digest, has_secret_password and form validations in RoR 3.1

If you haven't seen it yet, have a look at the latest railscast authentication in rails 3.1. He uses password_digest:string when generating the model and adds has_secret_password to the User model. He also adds some accessible_attributes, :password and :password_confirmation.
By using validates_confirmation_of :password we can make sure the password is confirmed before creating an instance. If I leave the password fields empty in my form I get this error message:
ruby-1.9.2-p180 :024 > u = User.new
=> #<User id: nil, name: nil, email: nil, password_digest: nil, created_at: nil, updated_at: nil>
ruby-1.9.2-p180 :027 > u.save
(0.4ms) SELECT 1 FROM "users" WHERE "users"."email" = '' LIMIT 1
=> false
ruby-1.9.2-p180 :028 > u.errors.full_messages
=> ["Password digest can't be blank"]
Of course we don't want to call the password field "Password digest" when communicating with our users. How do I change this error message?
Bonus question
When using validates_confirmation_of and using mismatching passwords I get two error messages telling me about it, and only the :password label and input tags are surrounded with fields_with_errors divs. If the passwords don't match I want to also highlight the password_confirmation input, if possible remove it altogether from the password part.
Should I write my own method for checking password confirmation? If so, could you provide some small guidelines?
The official way to solve this is to add the following to your locale file:
en:
activerecord:
attributes:
user:
password_digest: 'Password'
The English locale file is located at config/locales/en.yml. You may have to restart your server for the changes to be loaded in your environment.
To get rid of password_digest message, you can modify the view:
<% #user.errors.messages.delete(:password_digest) -%>
You could override the human_attribute_name method and set your own humanized version of the password_digest attribute. Try something like this:
HUMANIZED_ATTRIBUTES = {
:password_digest => "Password"
}
def self.human_attribute_name(attr, options={})
HUMANIZED_ATTRIBUTES[attr.to_sym] || super
end
Then your error should look like this: "Password can't be blank"

Resources