2017-08-03 17 views
1

我正在使用https://github.com/taxweb/ransack_advanced_search來使用條件組搜索一個表。然而,我似乎無法得到這個工作(是的,我是新來的!)。taxweb ransack_advanced_search不會搜索

設置看起來很簡單,我按照說明操作,但搜索只是導致顯示所有記錄(如果確實發生的話)。任何幫助讚賞。

控制器:

class QuizzesController < ApplicationController 
before_action :set_quiz, only: [:show, :edit, :update, :destroy] 

def index 
@search = Quiz.search(params[:q]) 
@results = @search.result().includes(:category, :genre) 

if params[:tag] 
    @quizzes = Quiz.tagged_with(params[:tag]) 
else 
@quizzes = Quiz.all 
end 

end 

def new 
@quiz = Quiz.new 
end 

def create 
@quiz = Quiz.new(quiz_params) 

if @quiz.save 
     redirect_to quizzes_path 
    else 
     render "new" 
end 
end 

def edit 
    @quiz = Quiz.find(params[:id]) 
end 

def update 
     if @quiz.update_attributes(quiz_params) 
      redirect_to quizzes_path 
     else 
      render "edit" 
     end 
end 

def show 
    @quiz = Quiz.find(params[:id]) 
end 

def destroy 
    @quiz.destroy 
respond_to do |format| 
    format.html { redirect_to quizzes_url, notice: 'Post was successfully destroyed.' } 
    format.json { head :no_content } 
end 

end 

private 
    def set_quiz 
    @quiz = Quiz.find(params[:id]) 
    end 

def quiz_params 
params.require(:quiz).permit(:genre, :title, :image, :tags, :Actions, :q, :tag_list, :name, :category, :category_id, :genre_id, :tag_me, :sample_text, :name) 
end 

end 

型號:

class Quiz < ActiveRecord::Base 

def self.ransackable_associations(*) 
%w(category genre) 
end 

def self.ransackable_attributes(*) 
%w(category title genre tag_me) + _ransackers.keys 
end 

mount_uploader :image, ImageUploader 
belongs_to :category 
belongs_to :genre 
acts_as_taggable 


validates :title, :presence => {:message => "Can't be blank." } 
validates :image, :presence => {:message => "Can't be blank." } 
validates :tag_me, :presence => {:message => "Can't be blank." } 
validates :sample_text, :presence => {:message => "Can't be blank." } 
validates :tag_list, :presence => {:message => "Can't be blank." } 
validates :category_id, :presence => {:message => "Can't be blank." } 
validates :genre_id, :presence => {:message => "Can't be blank." } 

end 

途徑:

Rails.application.routes.draw do 
root "quizzes#index"  

get 'tags/:tag', to: 'quizzes#index', as: :tag 


resources :quizzes do 
    collection do 
    match :advanced_search, to: 'quizzes#index', via: :post 
    end 
end 


end 

application.html.erb(頭):

<!DOCTYPE html> 
<html lang="en"> 

<head> 

<meta charset="utf-8"> 
<meta http-equiv="X-UA-Compatible" content="IE=edge"> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<meta name="description" content=""> 
<meta name="author" content=""> 

<title>QuizTags</title> 

<!-- Bootstrap Core CSS --> 
<!-- <link href="css/bootstrap.min.css" rel="stylesheet"> --> 
<%= tinymce_assets %> 
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %> 
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> 
<%= csrf_meta_tags %> 
<%= yield(:ransack_advanced_search_setup) %> 

etc. 

index.html.erb:

<%= render partial: 'ransack_advanced_search/advanced_search', 
locals: { 
    search_url: advanced_search_quizzes_path, 
    redirect_path: quizzes_path 
} 
%> 



<table id="quizzes" class="table table-striped table-bordered table-responsive"> 
 
\t <thead> 
 
    <tr> 
 
    <th>Category</th> 
 
    <th>Genre</th> 
 
    <th>Title</th> 
 
    <th>Image</th> 
 
    <th>Tags</th> 
 
    <th>Actions</th> 
 
    </tr> 
 
    \t </thead> 
 
    <% @quizzes.each do |quiz| %> 
 
    <tr> 
 
    <td><%= quiz.category.name %></td> 
 
    <td><%= quiz.genre.name %></td> 
 
    <td><%= quiz.title %></td> 
 
    <td><%= image_tag quiz.image_url(:thumb).to_s %></td> 
 
    <td><%= quiz.tag_me %></td> 
 
    <td><%= link_to "View", quiz %> <br> <%= link_to 'Edit', edit_quiz_path(quiz) 
 
    %> <br> <%= link_to 'Delete', quiz, method: :delete, data: { confirm: 'Are you sure?' } %> </td> 
 
    </tr> 
 
    <% end %> 
 
    </table> 
 

 

 

 

 

 

非常感謝 - 感謝任何幫助。

回答

0

在您的控制器中更改@results並將其更改爲@quizzes並立即刪除if語句,以查看是否可以使其正常工作。

+0

感謝您對此追逐的幫助。不幸的是,沒有什麼區別。奇怪的是沒有錯誤 - 搜索似乎沒有執行。 – lee