【问题标题】:How to list objects on the same page with the same attribute?如何在同一页面上列出具有相同属性的对象?
【发布时间】:2016-07-01 22:30:45
【问题描述】:

如果用户单击代表categorization 的图标,如何在主页上仅列出具有相同categorizationchallenges

查看

<%= 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 %>

如果用户点击上面的link_to,那么应该只列出与相应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') }

【问题讨论】:

  • 你得到了什么错误?
  • TypeError (nil is not a symbol): @potashin 用于控制器中的线路

标签: ruby-on-rails ruby model-view-controller routes link-to


【解决方案1】:

我需要它是单数的:

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-01-30
  • 1970-01-01
  • 2021-08-11
  • 2020-02-20
  • 1970-01-01
  • 2017-02-27
  • 2016-01-04
  • 1970-01-01
相关资源
最近更新 更多