我正在使用https://github.com/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(head):
<!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">
<thead>
<tr>
<th>Category</th>
<th>Genre</th>
<th>Title</th>
<th>Image</th>
<th>Tags</th>
<th>Actions</th>
</tr>
</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 :(得分:0)
更改控制器中的@results
并将其更改为@quizzes
并暂时删除if语句,看看是否可以使其正常工作。