[RAILS] The devise error message is not displayed properly.

Overview

The devise error message didn't display well, so I figured out why. It's not a solution, but I've learned a little about how devicise works, so I'll leave it as a reminder.

solution

When calling a partial template, I changed the variable name in the template from resource to @ model name.

Error message


<%= render "devise/shared/error_messages", resource: resource %>
↓
<%= render "devise/shared/error_messages", resource: @profile %>

Since devise is a framework that can handle multiple model instances, it seems that resource can handle it well. ](Https://teratail.com/questions/139633) Actually, there seems to be another process, but if I changed this to @ model name, it worked.

Other code

routing

Since user registration and profile registration are in a wizard format, profile is processed by the'users / registration'controller.

routes.rb


Rails.application.routes.draw do
  devise_for :users, controllers: {
    registrations: 'users/registrations'
  }
  devise_scope :user do
    get 'profiles', to: 'users/registrations#new_profile'
    post 'profiles', to: 'users/registrations#create_profile'
  end
end

Table design

I'm using devise to store user information in the users and profiles tables.

schema.rb


#The users table and profiels table look like this.

  create_table "profiles", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
    t.string "favorite_beer"
    t.text "twitter_link"
    t.text "info"
    t.bigint "user_id", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["user_id"], name: "index_profiles_on_user_id"
  end

  
  create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
    t.string "nickname", null: false
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

input form

The view of profile registration looks like this. The error message is called in the partial template here, but it seems that the information in the profiles table could not be passed to it.

new_profile


<h2>Profile registration</h2>
<%= form_with model: @profile, local: true do |f| %>
  <%= render "devise/shared/error_messages", resource: @profile %>

  <div class="field">
    <%= f.label :avatar %><br />
    <%= f.file_field :avatar %>
  </div>

  <div class="field">
    <%= f.label :favorite_beer %><br />
    <%= f.text_field :favorite_beer, autofocus: true, autocomplete: "favorite_beer" %>
  </div>

  <div class="field">
    <%= f.label :twitter_link %><br />
    <%= f.text_field :twitter_link, autocomplete: "twitter_link" %>
  </div>

  <div class="field">
    <%= f.label :info %><br />
    <%= f.text_area :info %>
  </div>

  <div class="actions">
    <%= f.submit "sign up" %>
  </div>
<% end %>

<%= render "devise/shared/links" %>

Source of error message

The error message of devise called by the partial template is as follows

erb:divese/shared/_error_messages.html.erb


<% if resource.errors.any? %>
<%#To the resource of this ↑ part@Passed profile%>
  <div id="error_explanation">
    <h2>
      <%= I18n.t("errors.messages.not_saved", #The description around here is devise.ja.Described in yml
                 count: resource.errors.count,
                 resource: resource.class.model_name.human.downcase)
       %>
    </h2>
    <ul>
      <% resource.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
    </ul>
  </div>
<% end %>

A file that translates devise into Japanese

Since devise has already been translated into Japanese, the yml file is also translated into Japanese.

yml:config/locales/devise.ja.yml


ja:
  activerecord:
    attributes:
# ~abridgement~     
    models:
      user:User
      profile:profile#I added it because I wanted to translate it into Japanese as well.
# ~abridgement~
  errors:
    messages:
      already_confirmed:Is already registered. Please login.
      confirmation_period_expired:Has expired.%{period}You need to confirm by. Please make a new request.
      expired:Has expired. Please make a new request.
      not_found:Was not found.
      not_locked:Is not frozen.
#This is not_It seems that the saved place is called by the partial template
      not_saved:
        one:Because an error occurred%{resource}Was not saved.
        other: "%{count}Because an error occurred%{resource}Was not saved."

Summary

I would like to continue studying in the future. If this sentence contains a ridiculous misunderstanding, please point it out.

Recommended Posts

The devise error message is not displayed properly.
[Error] The app is not displayed in the production environment
[Rails] About the error that the image is not displayed in the production environment
GoogleMap is not displayed until the browser is reloaded
When the project is not displayed in eclipse
No error message is displayed when create action fails
"tx" is not bound error
[Rails] fields_for is not displayed
Error in production environment (The asset "~" is not present in the asset pipeline.)
The idea of cutting off when the error is not resolved
[Rails] When the layout change of devise is not reflected
[Java] final override error is not displayed at compile time
[Rails] What was the error message?
About the problem that the image is not displayed after AWS deployment
The repository ... is not signed error in docker build apt-get update
"" Application.js "is not present in the asset pipeline" error in Ruby on Rails
How the website is displayed on the screen
When submitting form, no error message is displayed even though validation failed
About the error message Invalid redeclaration of'***'
Image is not displayed in production environment
When UITextView is not displayed (Swift, Xcode)
Get the error message using the any? method
Add if not in Set, error message if
[Cloud9] Yay! You ’re on Rails! Is not displayed in the rails tutorial
[Rails] What to do when the Refile image is not displayed when writing the processing at the time of Routing Error
If the JDK version is correct in Eclipse but an error occurs or the compiler does not work properly
Possibility when deploying to EC2 but nothing is displayed in the error log
[Java] Are you reading the error message properly? [How to read the stack trace]
The list of installed apps is not displayed in getPackageManager (Android11 ​​/ API level 30)
ActiveSupport underscore is not the inverse of camelize
The road to Japaneseizing Rails devise error messages
[Rails] Let's translate the error message into Japanese
The public key for jenkins-2.249.1-1.1.noarch.rpm is not installed
[Java] Something is displayed as "-0.0" in the output
Ebean.update () is not executed in the inherited model.
I want to solve the problem that JS is not displayed properly unless reloaded when transitioning with Turbolinks: link_to
[Ruby on rails] When executing the heroku command, bash: heroku: command not found is displayed. [Rails tutorial]
[Android Studio] About the matter that the design view is not displayed when using TextClock
Rails must be exist error (@save is not executed)
Docker Compose does not start with docker.credentials.errors.InitializationError error message
PATH does not pass ... The cause is the character code
"This is the wrong package! Please install eslint-plugin-jsx-a11y" error
When changing user information using devise Settings on the edit screen when the password is not saved
[Rails] What to do when the view collapses when a message is displayed with the errors method
[Eclipse] Version 1.8.0_261 of the JVM is not suitable for this product. Version: 11 or greater is required Error