・ Ruby: 2.5.7 Rails: 5.2.4 ・ Vagrant: 2.2.7 -VirtualBox: 6.1 ・ OS: macOS Catalina
The following has been implemented.
・ Slim introduction ・ Implementation of posting function
application.rb
module Bookers2Debug
class Application < Rails::Application
config.load_defaults 5.2
#Postscript
config.action_view.field_error_proc = Proc.new do |html_tag, instance|
if instance.kind_of?(ActionView::Helpers::Tags::Label)
html_tag.html_safe
else
class_name = instance.object.class.name.underscore
method_name = instance.instance_variable_get(:@method_name)
"<div class=\"has-error\">#{html_tag}
<span class=\"help-block\">
#{I18n.t("activerecord.attributes.#{class_name}.#{method_name}")}
#{instance.error_message.first}
</span>
</div>".html_safe
end
end
end
end
if instance.kind_of?(ActionView::Helpers::Tags::Label)
html_tag.html_safe
else
class_name = instance.object.class.name.underscore
method_name = instance.instance_variable_get(:@method_name)
"<div class=\"has-error\">#{html_tag}
<span class=\"help-block\">
#{I18n.t("activerecord.attributes.#{class_name}.#{method_name}")}
#{instance.error_message.first}
</span>
</div>".html_safe
** ◎ Assign the instance class name to a variable **
class_name = instance.object.class.name.underscore
** ◎ Assign the method name of the instance to a variable **
method_name = instance.instance_variable_get(:@method_name)
** ◎ Create HTML for the error message part. ** **
"<div class=\"has-error\">#{html_tag}
<span class=\"help-block\">
#{I18n.t("activerecord.attributes.#{class_name}.#{method_name}")}
#{instance.error_message.first}
</span>
</div>".html_safe
If "Please enter a title" is displayed,
# {I18n.t ("activerecord.attributes. # {Class_name}. # {Method_name} ")}
In the part corresponding to the "title"
# {Instance.error_message.first}
is
It becomes the part corresponding to "Please enter".
Gemfile
#Postscript
gem 'rails-i18n'
Terminal
$ bundle
application.rb
module Bookers2Debug
class Application < Rails::Application
config.load_defaults 5.2
config.i18n.default_locale = :ja #Postscript
config.action_view.field_error_proc = Proc.new do |html_tag, instance|
if instance.kind_of?(ActionView::Helpers::Tags::Label)
html_tag.html_safe
else
class_name = instance.object.class.name.underscore
method_name = instance.instance_variable_get(:@method_name)
"<div class=\"has-error\">#{html_tag}
<span class=\"help-block\">
#{I18n.t("activerecord.attributes.#{class_name}.#{method_name}")}
#{instance.error_message.first}
</span>
</div>".html_safe
end
end
end
end
ja.yml
Terminal
$ touch config/locales/ja.yml
ja.yml
ja:
activerecord:
attributes:
book:
title:title
body:Text
Recommended Posts