类别&分页问题文章中的NoMethodError #index undefined method`total_pages'

时间:2017-01-16 03:17:42

标签: ruby-on-rails

打开索引页面,它的分页工作正常,但点击下拉菜单catetory,然后发生

`NoMethodError in Articles#index`

undefined method `total_pages' for #<Article::ActiveRecord_Relation:0x007fc32f16f180>
web-console运行@articles中的

可以查看数据。

error

的Gemfile:

source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.1'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use Puma as the app server
gem 'puma', '~> 3.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platform: :mri
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '~> 3.0.5'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'simple_form', '~> 3.4'
gem 'bootstrap-sass', '~> 3.3', '>= 3.3.7'
gem 'devise', '~> 4.2'
gem 'rucaptcha'
gem 'dalli', '~> 2.7', '>= 2.7.6'
gem 'redcarpet', '~> 3.4'
gem 'coderay', '~> 1.1', '>= 1.1.1'
gem 'will_paginate', '~> 3.1', '>= 3.1.5'
gem 'paperclip', '~> 5.1'

articles_controller:

class ArticlesController < ApplicationController

  before_action :authenticate_user!, except: [:index, :show]
  before_action :get_article, only: [:edit, :update, :show, :destroy]

  def index
    if params[:category].blank?
      @articles = Article.paginate(:page => params[:page], per_page: 3).order("created_at DESC")
    else
      @category_id = Category.find_by(name: params[:category]).id
      @articles = Article.where(category_id: @category_id)
    end
  end

  def new
    @article = Article.new
    @categories = Category.all.map{ |c| [c.name, c.id] }
  end

  def create
    @article = current_user.articles.new(article_params)
    if @article.save
      redirect_to @article
    else
      render 'new'
    end
  end

  def edit
    @categories = Category.all.map{ |c| [c.name, c.id] }
  end

  def update
    if @article.update(article_params)
      redirect_to @article
    else
      render 'edit'
    end
  end

  def show

  end

  def destroy
    if @article.destroy
      redirect_to root_path
    end
  end

  private

    def article_params
      params[:article].permit(:title, :body, :category_id, :images)
    end

    def get_article
      @article = Article.find(params[:id])
    end

end

index.html.erb:

    <div class="panel panel-default">
        <div class="panel-body">
            <ul class="list-group">
                <% if ! @articles.blank? %>
                    <% @articles.each do |articles| %>
                        <li class="list-group-item"><%= link_to articles.title, articles %></li>
                    <% end %>
                <% else %>
                    <div class="well text-capitalize">
                        there is not article yet.
                    </div>
                <% end %>
            </ul>
        </div>
        <div class="panel-footer text-center">
            <%= will_paginate @articles %>
        </div>
    </div>

application.html.erb:

    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <div class="navbar-header">
                <%= link_to 'Logo', root_path, class: "navbar-brand" %>
            </div>
            <div class="collapse navbar-collapse">
                <ul class="nav navbar-nav navbar-right text-capitalize">
                    <% if user_signed_in? %>
                        <li><%= link_to "new article", new_article_path %></li>
                        <li><%= link_to "sign out", destroy_user_session_path, method: :delete%></li>
                    <% else %>
                        <li><%= link_to "sign in", new_user_session_path %></li>
                        <li><%= link_to "sign up", new_user_registration_path %></li>
                    <% end %>

                    <li class="dropdown">
                        <a href="#", class="dropdown-toggle", data-toggle="dropdown" role="button",
                            aria-haspopup="true", aria-expanded="false">Categories</a>
                        <ul class="dropdown-menu">
                            <% Category.all.each do |category| %>
                                <li><%= link_to category.name, articles_path(category: category.name) %></li>
                            <% end %>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

1 个答案:

答案 0 :(得分:1)

当category_id存在时(即点击下拉列表),您没有应用分页。

变化

@articles = Article.where(category_id: @category_id)

@articles = Article.where(category_id: @category_id).paginate(:page => params[:page], per_page: 3)