Rails 4 Filter Boolean True

时间:2016-05-04 20:27:59

标签: ruby-on-rails ruby-on-rails-4

我有一个页面,我想链接到并过滤到布尔属性为true的所有记录。

<div class="filter btn btn-default btn-directory active"><%= link_to "Accepting", people_path(:filter_by => :search == true ), {:method => :get} %></div>

这不会丢失任何错误,但它也不会获取任何结果。

来自控制器:

def index
        @people = Person.all

        if params[:filter_by]
            @people = Person.where(:position => params[:filter_by])
        else
            @people = Person.all
        end
    end

1 个答案:

答案 0 :(得分:0)

假设您要获取person.search == true

所有值

people_path中发送一个参数,如下所示:

link_to "Accepting", people_path(filter_by: true)

然后,在PeopleController#index

def index
  Person.where("search = ?", params[:filter_by] == "true")
end