Is it a loss if you don't know? Rails [Super] 5 selections to find error mistakes to escape from beginners

Introduction

Even for errors such as "You can understand by reading the error message!" </ B>, when you are a super beginner, you can rely on Google teacher for the time being. This article is an article on how to deal with an error written for such "copy and paste error messages for the time being" </ B> level Ruby on Rails super beginners.

Example 1: Japaneseization of error messages does not work ... (NoMethodError (undefined method `deep_symbolize_keys')

Let's translate error messages into Japanese using I18n!

Reference: Japanese localization using I18n in Rails

Alright! I set the Japanese file downloaded from the web.

ja.yml



ja:
activerecord:
errors:
messages:
record_invalid: "Validation failed: %{errors}"
restrict_dependent_destroy:
has_one: "%{record}Cannot be deleted because it exists"
has_many: "%{record}Cannot be deleted because it exists"

that? Somehow there is an error ...

NoMethodError (undefined method `deep_symbolize_keys' for "String":String):

What do you mean?

Cause of error

The indentation in config> locales> ja.yml is incorrect and the i18n translation is not working.

Error resolution

The correct description is to lower the indent by two half-width spaces for each element. If you copy and paste, the indent may shift, so download the file with the command and place it under the config> locales folder.

#Download Japanese translation file template
wget https://raw.github.com/svenfuchs/rails-i18n/master/rails/locale/ja.yml -P config/locales/
ja:
  activerecord:
    errors:
      messages:
        record_invalid: "Validation failed: %{errors}"
        restrict_dependent_destroy:
          has_one: "%{record}Cannot be deleted because it exists"
          has_many: "%{record}Cannot be deleted because it exists"

reference

[NoMethodError (undefined method `deep_symbolize_keys' for" Hello ": String):](https://github.com/2016chubachi/tabitomo/wiki/NoMethodError- (undefined-method-% 60deep_symbolize_keys% 27-for-% 22% E3 % 81% 93% E3% 82% 93% E3% 81% AB% E3% 81% A1% E3% 81% AF% 22: String) :)

By default, Ruby on Rails displays error messages in English, so translate them into Japanese so that users can easily understand them.

How to make it compatible with Japanese with Rails

Japanese localization of Rails validation error message --Qiita

Example 2: View loaded with Partial is not displayed ... (Missing partial)

I am trying to read _comment.html.erb under comments from posts / index.html.erb.

Folder structure

app
 - views
    - posts
        - index.html.erb
    - comments
        - _comment.html.erb

ruby:posts/index.heml.erb


  <%= render partial: "comment" %>

that? An error…

Missing partial xxx/_yyy, application/_yyy with {:locale=>[:ja], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}.

Cause of error

The path of the partial view is specified incorrectly.

Solution

When rendering (reading) a partial view, specify the path with the folder name / view name. </ b>

ruby:posts/index.heml.erb


  <%= render partial: "comments/comment" %>

I didn't specify the folder hierarchy, so I couldn't read it.

Example 3: View cannot be displayed even after rails s ... (missing a template for this request format and variant.)

<Controller name>Controller#<Action name> is missing a template for this request format and variant. request.formats: ["text/html"] request.variant: []

Cause of error

There is no view corresponding to the controller action

Solution

app> views> Create "action name.html.erb" under the folder of controller name

  • Extension depends on applicable format such as .slim and .haml

Example 4: The code should be correct, but the program cannot be executed ... (undefined local variable or method `')

undefined local variable or method ` ' for #<xxxxx> (NameError)

What's wrong ...

ruby:posts/index.html.erb


   <%= render partial: "comment" %>

Cause of error

Actually, double-byte spaces are included in the file.

Solution

Delete double-byte space. Allows you to discover double-byte spaces in the IDE settings.

reference

What to do when ActionController :: RoutingError suddenly appears in the rails app after repair

Example 5: JavaScript doesn't load well ... (ExecJS :: RuntimeError unclosed regex)

Rails errors don't always depend on Ruby.

ExecJS::RuntimeError in Controller#Action
SyntaxError: missing / (unclosed regex)

What is unclosed regex ...?

Cause of error

The description of the app> assets> javascripts> .coffee file is incorrect. (The coding block is not closed, such as {}) Therefore, the part where the stylesheet is read in view (application.html.erb) is an error.

ruby:apps/view/layouts/application.html.erb


  <%= stylesheet_link_tag 'application', media: 'all' %>

Solution

Review the description of the coffee file. Delete unnecessary parts.

reference

Weird error "missing / (unclosed regex)"

Summary

How was it? It was too easy for Tsuyotsuyo engineers!

It's okay to google for the time being, but you can grow up from a super beginner even if you try to break down what the error message says once. We support your programming studies!

Recommended Posts