Search code examples
ruby-on-railsrubymodel-view-controllerrouteslink-to

How to list objects on the same page with the same attribute?


How to list on the home page only the challenges with the same categorization if a user clicks on the icon representing that categorization?

enter image description here

view

<%= link_to categorization_path(categorization: :adventure) do %>    
  <span class="glyphicon glyphicon-picture", id="challenge-category"></span>
<% end %>
<%= link_to categorization_path(categorization: :health) do %>    
  <span class="glyphicon glyphicon-heart", id="challenge-category"></span>
<% end %>
<%= link_to categorization_path(categorization: :work) do %>    
  <span class="glyphicon glyphicon-briefcase", id="challenge-category"></span>
<% end %>
<%= link_to categorization_path(categorization: :buy) do %>    
  <span class="glyphicon glyphicon-shopping-cart", id="challenge-category"></span>
<% end %>
<%= link_to categorization_path(categorization: :wacky) do %>    
  <span class="glyphicon glyphicon-wine-glass", id="challenge-category"></span>
<% end %>

If a user clicks on an above link_to then that should only list challenges with that respective categorization.

routes.rb

get ":categorization", to: "pages#home", as: 'categorization'

pages_controller.rb

def home
 @challenges = current_user.challenges.send(params[:categorization]).order("deadline ASC")
end

challenge.rb

CATEGORIZATION = ['adventure', 'health', 'work', 'buy', 'wacky']
scope :adventure,  -> { where(categorizations: 'Adventure') }
scope :health,  -> { where(categorizations: 'Health') }
scope :work,  -> { where(categorizations: 'Work') }
scope :buy,  -> { where(categorizations: 'Buy') }
scope :wacky,  -> { where(categorizations: 'Wacky') }

Solution

  • I needed it to be singular:

      scope :adventure,  -> { where(categorization: 'adventure') }
      scope :health,  -> { where(categorization: 'health') }
      scope :work,  -> { where(categorization: 'work') }
      scope :buy,  -> { where(categorization: 'buy') }
      scope :wacky,  -> { where(categorization: 'wacky') }