2017-07-30 5 views
0

J'essaye de rechercher par des étiquettes, et cela fonctionnait bien. Mais après avoir commencé à utiliser gem will_paginate pour la pagination, je ne peux pas le faire parce que je reçois cette erreur:méthode undefined `total_pages 'pour # <Advertisement :: ActiveRecord_Relation: 0x007fe4cdd7df98>

undefined method 'total_pages' for #<Advertisement::ActiveRecord_Relation:0x007fe4cdd7df98> 

Mais si je recherche une étiquette inconnue, il fonctionne très bien (afficher une liste vide d'annonces sur l'index page).

advertisemts_controller.rb

class AdvertisementsController < ApplicationController 
    before_action :authenticate_user! 
    before_action :set_advertisement, only: %i[edit update destroy] 

    def index 
    @advertisements = Advertisement.paginate(page: params[:page], 
    per_page: 2) 
    end 

    def show 
    @advertisement = Advertisement.find(params[:id]) 
    end 

    def new 
    @advertisement = Advertisement.new 
    end 

    def create 
    @advertisement = Advertisements::Create.call(advertisement_params) 

    if @advertisement.errors.blank? 
     redirect_to advertisement_path(@advertisement), 
       notice: 'The advertisement was successfully added.' 
    else 
     render action: 'new' 
    end 
    end 

    def update 
    if @advertisement.update(advertisement_params) 
     redirect_to @advertisement, notice: 'Advertisement was 
              successfully updated.' 
    else 
     render :edit 
    end 
    end 

    def destroy 
    @advertisement.destroy 
    redirect_to advertisements_path, notice: 'Advertisement was 
    successfully destroyed.' 
    end 

    private 

    def set_advertisement 
    @advertisement = current_user.advertisements.find(params[:id]) 
    end 

    def advertisement_params 
    params 
     .require(:advertisement) 
     .permit(:title, :description, :user_id, :tags) 
     .merge(user_id: current_user.id) 
    end 
end 

search_queries_controller.rb

class SearchQueriesController < ApplicationController 
    def search_by_tag 
    @advertisements = Advertisement.find_by_tags(tags_params) 

    render 'advertisements/index' 
    end 

    private 

    def tags_params 
    params.fetch(:tags, '') 
    end 
end 

advertisement.rb

class Advertisement < ApplicationRecord 
    has_many :advertisement_tags, dependent: :destroy 
    has_many :comments 
    has_many :tags, through: :advertisement_tags 
    belongs_to :user 

    validates :title, 
      :description, 
      presence: true 

    def self.find_by_tags(tags) 
    Advertisement.joins(:tags).where('tags.tag_name IN (?)', 
             tags.split(/[\s,']/)) 
    end 
end 

Je ne sais pas comment résoudre ce problème, je me sers ruby-2.3.4 et rails-5.1.2, will_paginate-3.1.

Répondre

0

C'est parce que

def search_by_tag 
    @advertisements = Advertisement.find_by_tags(tags_params) 

    render 'advertisements/index' 
end 

ne remet pas paginate méthode sur la relation, mais rend la vue qui utilise la pagination. Vous pouvez même remarquer que Advertisement.all.paginate(page: 1).total_pages fonctionne, mais pas Advertisement.all.total_pages.

Quelque chose comme

def search_by_tag 
    @advertisements = Advertisement 
    .find_by_tags(tags_params) 
    .paginate(page: 1, per_page: 2) 

    render 'advertisements/index' 
end 

devrait résoudre le problème

+0

Merci! Cela fonctionne bien maintenant –