LoginSignup
1
1

erbからslimに変換する際の`File.exists?` メソッド使用についての解決方法

Last updated at Posted at 2024-06-24

はじめに

erbからslimに変換しようとしてエラーで詰まって調べてあまり有用な記事がなかったため
作成することにします(自分見直し用でもあります!)

起きたこと

dockerコンテナ内で

erb2slim app/views/users/ app/views/users/ -d

をしてapp/views/users/内のerbファイルを一括でslimファイルに変換しようとしたところ

NoMethodError: undefined method `exists?' for File:Class
  Use --trace for backtrace.

とエラーが出て、できなかったので

erb2slim app/views/users/ app/views/users/ -d --trace

--traceオプションをつけてエラーが起きているファイルを特定したところ

/usr/local/bundle/gems/html2slim-0.2.0/lib/html2slim/converter.rb:17:in initialize': undefined method exists?' for File:Class (NoMethodError)
      erb = File.exists?(file) ? open(file).read : file
                ^^^^^^^^
Did you mean?  exist?
        from /usr/local/bundle/gems/html2slim-0.2.0/lib/html2slim.rb:9:in new'
        from /usr/local/bundle/gems/html2slim-0.2.0/lib/html2slim.rb:9:in convert!'
        from /usr/local/bundle/gems/html2slim-0.2.0/lib/html2slim/command.rb:98:in _process'
        from /usr/local/bundle/gems/html2slim-0.2.0/lib/html2slim/command.rb:66:in block in process!'
        from /usr/local/bundle/gems/html2slim-0.2.0/lib/html2slim/command.rb:66:in each'
        from /usr/local/bundle/gems/html2slim-0.2.0/lib/html2slim/command.rb:66:in process!'
        from /usr/local/bundle/gems/html2slim-0.2.0/lib/html2slim/command.rb:15:in run'
        from /usr/local/bundle/gems/html2slim-0.2.0/bin/erb2slim:7:in <top (required)>'
        from /usr/local/bundle/bin/erb2slim:25:in load'
        from /usr/local/bundle/bin/erb2slim:25:in <main>'

エラーで出ている該当のファイルがわかりました。

解決方法

上記のエラーメッセージから、問題は html2slim gem の中にあることがわかります。
/usr/local/bundle/gems/html2slim-0.2.0/lib/html2slim/converter.rb ファイルの
17行目で File.exists? メソッドが使用されています。

この問題を解決するには、以下の手順で:

1. gem のソースコードを編集する:

cd /usr/local/bundle/gems/html2slim-0.2.0

2. lib/html2slim/converter.rb ファイルを編集します:

vim lib/html2slim/converter.rb

または他のテキストエディタを使用してください。

3. 17行目を以下のように変更します:

変更前:

erb = File.exists?(file) ? open(file).read : file

変更後:

erb = File.exist?(file) ? open(file).read : file

4. ファイルを保存して閉じます。

終わりに

ruby3.0より上のバージョンだとexists?は使用できないため、エラーが起きているようなので
同じ問題で引っかかった方はぜひ試してみてください!

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1