I've a project that implements devise and I'm having trouble overriding the Passwords controller's messages.
When a wrong email address is entered by the user, Devise by default gives
Unable to find user with email {email}
I cant find the key to override this message in the devise.en.yml and can't find anything with the similar message on the devise repo as well.
Do I have an option other than to override the controller? Any help is appreciated.
Thanks
Add a new locale having the following structure:
en:
devise_token_auth:
passwords:
user_not_found: "Your custom message"
This message will be displayed when an invalid email is passed in the forgot password form.
Originally this message comes from the devise_token_auth gem. But if you have the same locale in your locale files, it will override the gem's locale.
It doesn't matter which *.en.yml file you will put this locale into. It can be devise.en.yml, or you can add a new file called devise_token_auth.en.yml. Only the structure matters.
as described in the following link you can generate the devise controller with
rails generate devise:controllers user
then you can over-ride the create or update action. If you include super in the new action, it will call the parent controller action and then execute the code.
You can also over-ride the devise messages as explained in this post, you just need to create that locale in your settings
en:
devise_token_auth:
sessions:
not_confirmed: "your message overwritten"
Related
I am allowing users to change their email in Devise.
Since this commit : https://github.com/heartcombo/devise/commit/ce071502eeb5c5c5199436f6694bfb6103010c4a it is possible to send an alternative email than the usual "Welcome" Registration email.
Devise mailer now includes a specific method for this: https://github.com/heartcombo/devise/blob/master/app/mailers/devise/mailer.rb
I have then created 2 different views in my my_model/views/mailer folder: email_changed.html.erb and email_changed.text.erb
Though this alternative email is never picked. The original Welcome email is sent instead...
Am I missing something ?
===
My Devise version is 4.7.3 (also I am coming from an old version)
and my devise initializer has config.reconfirmable = true
I have seen this hack but I guess it shouldn't need be used anymore : How to send two different emails for devise confirmable and devise reconfirmable?
Is that my_model/views/mailer folder right? according to the code (*) it should be views/my_model/mailer.
https://github.com/heartcombo/devise/blob/45b831c4ea5a35914037bd27fe88b76d7b3683a4/lib/devise/mailers/helpers.rb#L66
I would suggest you to copy that helper file inside your project so you can put some byebug statement and properly debug it.
I am building a car rental app with Ruby on Rails and currently I have been coding with English. Basically I have an user and car model, where user can login, sign in, log out and list their car.
I would like to implement another language allowing the user to select English or Spanish from a dropdown. I have default error messages returns from the controller actions such as "can't be blank, already used" etc. And I also have custom JS messages, such as if user successfully adds a car, jQuery returns "your car has been published". Lastly, I also have Flash messages. But I do not know how should I handle errors, custom JS and Flash messages in the YML file according to user's language selection.
It actually really depends on the structure of your YAML file, for example, for ActiveRecord error messages:
it:
errors:
format: "%{attribute} %{message}"
messages: &errors_messages
empty: "non può essere vuoto"
blank: "non può essere lasciato in bianco"
If you want to access I18n (a.k.a Internationalization) outside of views, you can use I18n.t(your_key), which is in fact the helper you have access to in your views. So for example, if you wish to translate a notice in a controller you could be doing something like:
def your_action
redirect_to other_path, notice: I18n.t("notices.successful_action")
end
And then having in your YAML the correspondent structure:
en:
notices:
successful_action: "You did it!"
Hope this helps!
I do not know how should I handle error, custom js and flash messages in yml file according to user's language selection
For Rails app-related translations in general, including error messages, the Rails i18n gem has you covered. You can see what languages have been translated in the gem's locale file.
For specifically overriding error messages or creating new ones, see The Rails Guide sections on looking up translations and translations for ActiveRecord models to see what keys in your YAML files you should be putting your translations under.
For flash messages, have a look at this StackOverflow question to see how you can use lazy lookups (eg t('.notice')) in your controllers.
For the ability to use translations in Javascript files in the same way you do in Ruby (eg I18n.t("my_translation"), and be able to manage those translations in your YAML files with the rest of your Rails translations, have a look at the i18n-js gem. Setup instructions are in the README file.
i already know the mailer function in the rails and succeed in sending email to my customer. now im interested in the smseasy gem. i have been looking in the internet on how to setup it but i cant find any tutorial. i did read the readme in the https://github.com/preston/sms-easy but i didnt understand it.
# Override the default "from" address with config/initializers/sms-easy.rb
SMSEasy::Client.config['from_address'] = "noreply#example.com"
# Or, you can completely copy sms-easy.yml to your app (http://github.com/preston/sms-easy/blob/master/templates/sms-easy.yml), change it to your liking, and override the default configuration with:
SMSEasy::Client.configure(YAML.load(...))
# Your apps existing ActionMailer configuration will be used. :)
# Create the client
easy = SMSEasy::Client.new
# Deliver a simple message.
easy.deliver("5551234567", "verizon", "Hey!")
where does i wrote this code.
if any of you can help to teach me how to setup it. it would be nice.
The following code belongs in one of your controller actions that you want to trigger sending out the SMS:
# Create the client
easy = SMSEasy::Client.new
# Deliver a simple message.
easy.deliver("5551234567", "verizon", "Hey!")
under config/initializers directory you can create a file called sms-easy.rb and put the following line of code to set a from field which is supposed to override the default that you've set up with your mailer settings but it doesn't work for me currently, if i figure it out I can update the answer:
# Override the default "from" address with config/initializers/sms-easy.rb
SMSEasy::Client.config['from_address'] = "noreply#example.com"
I have a controller which inherited from ApplicationController
where I send reset password insturctions, is there any way to specify template?
#user=User.find_by_email(email);
if #user.nil?
render :status=>404, :json=>{:message=>"User not found or email format is invalid."}
return
else
#user.devise_mailer.reset_password_instructions(#user).deliver
render :status=>200, :json=>{:message=>"Reset password instructions have been sent."}
end
As you see in the snippet above, #user.devise_mailer.reset_password_instructions(#user).deliver
Sends reset password instructions, but it takes default email template, which I don't wanna change. I need to create another template and somehow specify to use it
Create your own mailer then. See http://guides.rubyonrails.org/action_mailer_basics.html for details. Once you have a new mailer, in the controller, instead of:
#user.devise_mailer.reset_password_instructions(#user).deliver
you'd do something like:
YourMailer.reset_password_instructions(#user).deliver
You may want to reuse parts of Devise in your mailer: check out devise sources for that: https://github.com/plataformatec/devise/blob/master/lib/devise/mailers/helpers.rb
Another thing you may try is redefining devise template by putting a file named app/views/devise/mailer/reset_password_instructions.html.erb in your application directory. Of course you can base your implementation on devise sources as well: https://github.com/plataformatec/devise/blob/master/app/views/devise/mailer/reset_password_instructions.html.erb
I am trying to have the Devise log in form be on my home page.
When I just copy the form over, of course I get errors because the 'resource' variables and etc are not set in the action.
I found this solution on the internet: http://pupeno.com/blog/show-a-devise-log-in-form-in-another-page/
However, his solution is to set the needed variables in a module called ContentHelper.
Where do I put this code? I tried putting it in the initializers, but I still get the error about 'resource' variable not existing
See if this entry in the devise wiki helps you!
https://github.com/plataformatec/devise/wiki/How-To:-Display-a-custom-sign_in-form-anywhere-in-your-app
Put the module in a file in app/helpers/content_helper.rb. If you still get errors add helper :content to your controller.