[英]Passing current_scopes from the has_scopes gem as a param
I've been struggling with this problem for several days. 几天来我一直在努力解决这个问题。 I'm a lawyer and I'm building a Q&A type site for legal advice (yups, I'm a relative newbie to rails :-)).
我是一名律师,我正在建立一个Q&A类型的网站以获取法律建议(是的,我是铁路的相对新手:-))。
I want users to be able to filter questions by category (ie company law, IP etc etc) and then be able to sort them on a secondary basis by most recent or popular etc. 我希望用户能够按类别(即公司法,IP等)过滤问题,然后能够在次要基础上按最近或最受欢迎等方式对其进行排序。
I've tried a number of approaches and ultimately, I've been unable to find a way to pass the current category selected as another param when trying to sort by recent, popular etc. 我已经尝试了很多方法,最终,在尝试按最近,流行等排序时,我一直无法找到将当前类别作为另一个参数传递的方法。
I've just started using the has_scopes gem and I see from the documentation that there is a method called 'current_scopes'. 我刚刚开始使用has_scopes gem,我从文档中看到有一个名为'current_scopes'的方法。 I can't seem to work out how to pass current_scopes in the view as a param where the recent or popular scope can then be called on it.
我似乎无法弄清楚如何在视图中传递current_scopes作为一个参数,然后可以在其上调用最近或流行的范围。 Logically, I'm assuming that current_scopes needs to be assigned to a variable in the controller, but again, can't work out how to then pass this in the view as a param.
从逻辑上讲,我假设需要将current_scopes分配给控制器中的变量,但是再次无法确定如何在视图中将其作为参数传递。
My current code: 我目前的代码:
has_many :comments, dependent: :destroy
belongs_to :user, :counter_cache => true
belongs_to :category, :counter_cache => true
scope :recent, ->{ order("created_at DESC")}
scope :popular, -> { order("comments_count DESC")}
scope :unanswered, -> {where(comments_count: 0)}
scope :category, -> category_id {where(:category_id => category_id)}
has_scope :category
has_scope :recent, :type => :boolean
has_scope :popular, :type => :boolean
has_scope :unanswered, :type => :boolean
def index
@posts = @q.result.includes(:comments).order("created_at DESC") #@q comes from using Ransack gem and applying in the application controller
@posts = apply_scopes(Post).all
@scope = current_scopes
<h3>Filter by: </h3>
<ul class="side-nav fixed" role="navigation" title="Link List">
<li role="menuitem"><%= link_to "Show all", root_path %></li>
<li role="menuitem"><%= link_to "Corporate", category: 1 %></li>
<li role="menuitem"><%= link_to "Intellectual Property", category: 2 %></li>
<li role="menuitem"><%= link_to "Employment", category: 3 %></li>
<li role="menuitem"><%= link_to "Commercial", category: 4 %></li>
<li role="menuitem"><%= link_to "Real Estate", category: 5 %></li>
<li role="menuitem"><%= link_to "Venture Capital", category: 6 %></li>
</ul>
<h3>Sort by:</h3>
<li> <%= link_to "Most Recent", :recent => true %> </li>
<li> <%= link_to "Most Popular", :popular => true %></li>
<li> <%= link_to "Unanswered", :unanswered => true %></li>
Thank you and really appreciate any assistance with this matter. 谢谢,非常感谢您对此事的任何帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.