1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

kaminariでbootstrap4を適応したファイルが作成されない時の対処方法

Last updated at Posted at 2023-02-06

状況

  • ruby: 2.4.5
  • rails: 5.0.0.1
  • kaminari-core-1.2.2

以下のコマンド実行するとエラー文が出る

rails g kaminari:views bootstrap4

/usr/local/bundle/gems/kaminari-core-1.2.2/lib/generators/kaminari/views_generator.rb:117:in `get_files_in_master': private method `open' called for URI:Module (NoMethodError)

views_generator.rb 一部抜粋

views_generator.rb
module GitHubApiHelper
      require 'open-uri'

      def get_files_in_master
        master_tree_sha = URI.open('https://api.github.com/repos/amatsuda/kaminari_themes/git/refs/heads/master') do |json|
          ActiveSupport::JSON.decode(json.read)['object']['sha']
        end
        URI.open('https:i//api.github.com/repos/amatsuda/kaminari_themes/git/trees/' + master_tree_sha + '?recursive=1') do |json|
          blobs = ActiveSupport::JSON.decode(json.read)['tree'].find_all {|i| i['type'] == 'blob' }
          blobs.map do |blob|
            [blob['path'], blob['sha']]
          end
        end
      end
      module_function :get_files_in_master

      def get_content_for(path)
        URI.open('https://api.github.com/repos/amatsuda/kaminari_themes/contents/' + path) do |json|
          Base64.decode64(ActiveSupport::JSON.decode(json.read)['content'])
        end
      end
      module_function :get_content_for
    end

解決策

Rubyバージョン2.6よりURI.open('url')

それ以下はopen('url')

Rubyのバージョンを上げるかviews_generator.rbを修正する。
もしくはkaminari 1.1.1を指定する。

今回はviews_generator.rbを修正した

修正後 views_generator.rb

 module GitHubApiHelper
      require 'open-uri'

      def get_files_in_master
        master_tree_sha = open('https://api.github.com/repos/amatsuda/kaminari_themes/git/refs/heads/master') do |json|
          ActiveSupport::JSON.decode(json.read)['object']['sha']
        end
        open('https:i//api.github.com/repos/amatsuda/kaminari_themes/git/trees/' + master_tree_sha + '?recursive=1') do |json|
          blobs = ActiveSupport::JSON.decode(json.read)['tree'].find_all {|i| i['type'] == 'blob' }
          blobs.map do |blob|
            [blob['path'], blob['sha']]
          end
        end
      end
      module_function :get_files_in_master

      def get_content_for(path)
        open('https://api.github.com/repos/amatsuda/kaminari_themes/contents/' + path) do |json|
          Base64.decode64(ActiveSupport::JSON.decode(json.read)['content'])
        end
      end
      module_function :get_content_for
    end

プロジェクト配下にviews_generator.rbを作成して以下のコマンドをDockerfile内、RUN bundle install後に追加した。

COPY views_generator.rb /usr/local/bundle/gems/kaminari-core-1.2.2/lib/generators/kaminari/

Ruby 2.4.5 OpenURI

Ruby 2.6 OpenURI

1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?