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.
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.
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
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
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" %>
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 %>
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."
I would like to continue studying in the future. If this sentence contains a ridiculous misunderstanding, please point it out.
Recommended Posts