【问题标题】:Rails: Autoloading lib not workingRails:自动加载库不起作用
【发布时间】:2023-03-27 00:50:01
【问题描述】:

由于某种原因,我的自动加载器无法正常工作,我遵循了一些教程,这是我的 config\application.rb 文件的样子

require File.expand_path('../boot', __FILE__)

require 'rails/all'

Bundler.require(:default, Rails.env) if defined?(Bundler)

module Quotes
  class Application < Rails::Application

    # Custom directories with classes and modules you want to be autoloadable.
    # config.autoload_paths += %W(#{config.root}/extras)
        config.autoload_paths += %W(#{config.root}/lib)

我收到此错误: 未初始化的常量 ActionView::CompiledTemplates::PaginationListLinkRenderer

这是我的 lib\paginationlistlinkrenderer.rb 代码

class PaginationListLinkRenderer < WillPaginate::ViewHelpers::LinkRenderer

    protected
    ...
    ...

end

这是我的 index.html.erb

<div id="img_content">
    <%= render @posts%>
</div>

<%= will_paginate(@posts, :renderer => PaginationListLinkRenderer) %>
<%= link_to "New Quote", new_post_path %>

我只需要预加载这个文件,以便我的控制器能够识别它。有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails config loader


    【解决方案1】:

    在您的 config/initializers 文件夹中创建一个名为 pagination.rb 的文件并包含以下内容。重新启动,它应该可以工作。

    module WillPaginate::ViewHelpers
        # default options that can be overridden on the global level
        @@pagination_options = {
          :class        => 'pagination',
          :previous_label   => '&laquo; Previous',
          :next_label   => 'Next &raquo;',
          :inner_window => 2, # links around the current page
          :outer_window => -1, # links around beginning and end
          :limit        => 5,
          :separator    => ' ', # single space is friendly to spiders and non-graphic browsers
          :param_name   => :page,
          :params       => nil,
          :gap          => "...",
          :renderer     => '::PaginationListLinkRenderer',
          :page_links   => true,
          :container    => true
        }
        mattr_reader :pagination_options
    end
    

    将你的 Lib 分页文件更改为...

    pagination_list_link_renderer.rb
    

    确保您拥有最新版本的 Will_Pagination。版本 3 Pre

    【讨论】:

    【解决方案2】:

    只是改变:

    lib\paginationlistlinkrenderer.rb 代码

    class PaginationListLinkRenderer < WillPaginate::ViewHelpers::LinkRenderer
    
        protected
        ...
        ...
    
    end
    

    class PaginationListLinkRenderer < WillPaginate::ActionView::LinkRenderer
    
        protected
        ...
        ...
    
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-31
      • 2017-11-26
      • 2015-04-09
      • 1970-01-01
      • 1970-01-01
      • 2016-08-01
      相关资源
      最近更新 更多