使用 Ransack 分层导航栏简单搜索以进一步确定范围
Layering navbar simple search with Ransack for further scoping
我的导航 header 上有一个搜索栏,用户可以在其中搜索文章的标题。这会将他们路由到 articles/browse.html.erb 页面,在那里他们可以按类别和作者姓名进一步缩小结果范围。
我已按照 this 教程进行导航栏搜索。我试图使用 Ransack 允许在浏览页面上进行进一步过滤。但是,我很难将这两种搜索方法与以下问题结合起来:
1) 导航栏搜索工作正常并且 returns 正确的结果,但是当我再次按类别名称进一步过滤结果时,我得到 EDIT 0 个结果 所有结果。
2) 当我使用文章标题在浏览页面上进行初始搜索时,搜索成功了。但是,当我通过 category/author 名称进一步缩小结果范围时,Ransack 似乎不起作用。 (编辑:通过浏览器中的以下编辑按类别过滤搜索。html.erb,但不适用于作者姓名)
浏览。html.erb
<%= search_form_for @q, url: browse_articles_path, method: :get do |f| %>
<div class="form-group">
<div class = "row">
<div class="col-md-12 col-sm-12 col-xs-12">
<%= f.search_field :title_cont, placeholder: "Find Articles", class: "form-control" %>
</div>
</div>
</div>
<div class="form-group">
<div class ="row">
<div class="col-md-6 col-sm-6 col-xs-6">
<%= f.select :categories_name_cont, options_from_collection_for_select(Category.all, //EDIT change "id" to// "name", "name", @q.categories_name_eq),
{ :prompt => "Any Category" },
{ class: "form-control" } %>
</div>
<div class="col-md-6 col-sm-6 col-xs-6">
<%= f.search_field :users_name_cont, class: "form-control", placeholder: "Author" %>
</div>
</div>
</div>
<div class="form-group">
<%= f.submit class: 'btn btn-primary btn-md' %>
</div>
<% end %>
<% if params[:search] %>
<%= render @articles %>
<% elsif params[:q] %>
<%= render @articles_morefilter %>
<% else %>
<%= render @articles %>
<% end %>
articles_controller.rb
def browse
if params[:search]
@articles = Article.search(params[:search])
@categories = Category.joins(:articles).where('articles.title LIKE ?', "%#{params[:search]}%").uniq
else
@articles = Article.published.order("cached_votes_up DESC")
@categories = Category.with_articles.order('name ASC').all
end
@q = Article.ransack(params[:q])
@articles_morefilter = @q.result.includes(:categories, :users)
end
编辑模型:article.rb
class Article < ApplicationRecord
belongs_to :user
has_many :article_categories
has_many :categories, through: :article_categories
def self.search(search)
where('title LIKE ?', "%#{search}%")
end
end
routes.rb
resources :articles do
collection do
get 'browse'
end
end
架构:
create_table "articles", force: :cascade do |t|
t.string "title"
t.text "description"
t.bigint "user_id"
end
create_table "users", force: :cascade do |t|
t.string "name"
end
_导航.html.erb
<%= form_tag(browse_articles_path, :method => 'get', :id => "articles_search") do %>
<div class="form-group">
<%= text_field_tag :search, params[:search], placeholder: "Search", class: "form-control" %>
</div>
<button type="submit" class="btn btn-default">Search</button>
<% end %>
你应该可以简单地搜索所有的字段。 (基于当前 browse.html.erb)
def browse
@q = Article.ransack(params[:q])
@articles = @q.result.includes(:categories, :users)
end
我的导航 header 上有一个搜索栏,用户可以在其中搜索文章的标题。这会将他们路由到 articles/browse.html.erb 页面,在那里他们可以按类别和作者姓名进一步缩小结果范围。
我已按照 this 教程进行导航栏搜索。我试图使用 Ransack 允许在浏览页面上进行进一步过滤。但是,我很难将这两种搜索方法与以下问题结合起来:
1) 导航栏搜索工作正常并且 returns 正确的结果,但是当我再次按类别名称进一步过滤结果时,我得到 EDIT 0 个结果 所有结果。
2) 当我使用文章标题在浏览页面上进行初始搜索时,搜索成功了。但是,当我通过 category/author 名称进一步缩小结果范围时,Ransack 似乎不起作用。 (编辑:通过浏览器中的以下编辑按类别过滤搜索。html.erb,但不适用于作者姓名)
浏览。html.erb
<%= search_form_for @q, url: browse_articles_path, method: :get do |f| %>
<div class="form-group">
<div class = "row">
<div class="col-md-12 col-sm-12 col-xs-12">
<%= f.search_field :title_cont, placeholder: "Find Articles", class: "form-control" %>
</div>
</div>
</div>
<div class="form-group">
<div class ="row">
<div class="col-md-6 col-sm-6 col-xs-6">
<%= f.select :categories_name_cont, options_from_collection_for_select(Category.all, //EDIT change "id" to// "name", "name", @q.categories_name_eq),
{ :prompt => "Any Category" },
{ class: "form-control" } %>
</div>
<div class="col-md-6 col-sm-6 col-xs-6">
<%= f.search_field :users_name_cont, class: "form-control", placeholder: "Author" %>
</div>
</div>
</div>
<div class="form-group">
<%= f.submit class: 'btn btn-primary btn-md' %>
</div>
<% end %>
<% if params[:search] %>
<%= render @articles %>
<% elsif params[:q] %>
<%= render @articles_morefilter %>
<% else %>
<%= render @articles %>
<% end %>
articles_controller.rb
def browse
if params[:search]
@articles = Article.search(params[:search])
@categories = Category.joins(:articles).where('articles.title LIKE ?', "%#{params[:search]}%").uniq
else
@articles = Article.published.order("cached_votes_up DESC")
@categories = Category.with_articles.order('name ASC').all
end
@q = Article.ransack(params[:q])
@articles_morefilter = @q.result.includes(:categories, :users)
end
编辑模型:article.rb
class Article < ApplicationRecord
belongs_to :user
has_many :article_categories
has_many :categories, through: :article_categories
def self.search(search)
where('title LIKE ?', "%#{search}%")
end
end
routes.rb
resources :articles do
collection do
get 'browse'
end
end
架构:
create_table "articles", force: :cascade do |t|
t.string "title"
t.text "description"
t.bigint "user_id"
end
create_table "users", force: :cascade do |t|
t.string "name"
end
_导航.html.erb
<%= form_tag(browse_articles_path, :method => 'get', :id => "articles_search") do %>
<div class="form-group">
<%= text_field_tag :search, params[:search], placeholder: "Search", class: "form-control" %>
</div>
<button type="submit" class="btn btn-default">Search</button>
<% end %>
你应该可以简单地搜索所有的字段。 (基于当前 browse.html.erb)
def browse
@q = Article.ransack(params[:q])
@articles = @q.result.includes(:categories, :users)
end