简体   繁体   中英

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 :-)).

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.

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'. 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. 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.

My current code:

post.rb

    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)}

posts_controller.rb

  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

_sidebar.html.erb

    <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.

For anyone else looking for the answer to this question, the current_scopes value is only available after apply_scopes has been called. The issue is documented here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM