Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何按类别进行搜索?_Ruby On Rails - Fatal编程技术网

Ruby on rails 如何按类别进行搜索?

Ruby on rails 如何按类别进行搜索?,ruby-on-rails,Ruby On Rails,我有两个模型post和categorywitn-habtm关联。如何在查询中运行indexaction-in-posts\u-controller 单击某个类别,她将以相同的动作返回这些类别中的示例位置 class PostsController < ApplicationController def index @posts = Post.where(status: true) @categories = Category.all end 在表单中添加搜索字段,

我有两个模型
post
category
witn-habtm关联。如何在查询中运行
index
action-in-posts\u-controller

单击某个类别,她将以相同的动作返回这些类别中的示例位置

class PostsController < ApplicationController
  def index
    @posts = Post.where(status: true)
    @categories = Category.all
  end
在表单中添加搜索字段,并使用一些字符串调用相同的
index
操作, 然后在控制器中

def index
   if params[:search]
       @categories = Category.where(name: params[:search])
   else
        @posts = Post.where(status: true)
        @categories = Category.all   
   end
end

对不起,我没听懂你的英语。你能从你的视图中显示更多的代码吗?使类别名称成为一个链接,当你点击带有该类别名称的调用索引操作时,请看我的更新答案
= link_to category.name, {:controller => "controllername", :action => "index", :search=> category.name}
def index
   if params[:search]
       @categories = Category.where(name: params[:search])
   else
        @posts = Post.where(status: true)
        @categories = Category.all   
   end
end