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