首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在控制器动作中只显示具有相同属性的对象?

在控制器动作中只显示具有相同属性的对象?
EN

Stack Overflow用户
提问于 2016-06-13 20:17:05
回答 2查看 44关注 0票数 1
代码语言:javascript
复制
<%= link_to categorization_path(categorization: :adventure) do %>    
  <span class="glyphicon glyphicon-picture", id="challenge-category"></span>
<% end %>
# There are other categories such as health, work, wacky, etc

routes.rb

代码语言:javascript
复制
get ":categorization", to: "pages#home", as: 'categorization'

此时,如果用户单击上面的link_to,我只想显示对categorization: adventure属性的挑战。

我需要在pages#home中放些什么才能让它工作呢?

pages_controller.rb

代码语言:javascript
复制
def home
 @challenges = current_user.challenges.order("deadline ASC")
 #How to only show one of the :categorization challenges if URL is root_path/:categorization?
end

challenge.rb

代码语言:javascript
复制
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') }
scope :goal,  -> { where(categories: 'Goal') }
scope :habit,  -> { where(categories: 'Habit') }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-07-07 04:14:36

如果URL是root_path/:分类法,如何仅显示:分类挑战之一?

默认的@challenges已经返回一个有序的ActiveRecord::Relation对象。因此,您可以将一个scope链接到它。

代码语言:javascript
复制
class PagesController < ApplicationController
  # 1) using Rails method: `Object#try`
  # http://api.rubyonrails.org/classes/Object.html#method-i-try
  #
  def home
    # default challenges
    @challenges = current_user.challenges.order("deadline ASC")

    if home_params[:categorization]
      # `try` scope categorization param
      # `try` returns nil if param is invalid
      @challenges = @challenges.try(home_params[:categorization])

      # even if no results, empty AR object still returned
      #   => #<ActiveRecord::Relation []>
      #
      unless @challenges.is_a?(ActiveRecord::Relation)
        # do whatever here; remove placeholder on next line:
        raise "invalid categorization => #{home_params[:categorization]}"
      end
    end
  end

  # --OR--

  # 2) using Ruby method: `Object#send`
  # http://ruby-doc.org/core-2.3.1/Object.html#method-i-send
  #
  def home
    # default challenges
    @challenges = current_user.challenges.order("deadline ASC")

    if home_params[:categorization]
      @challenges = @challenges.send(home_params[:categorization])
    end

  rescue NoMethodError
    # do whatever here; remove placeholder on next line:
    raise "invalid categorization => #{home_params[:categorization]}"
  end



  private

  def home_params
    params.permit(:categorization)
  end
end

请参阅:http://guides.rubyonrails.org/active_record_querying.html#scopes

请参阅:http://guides.rubyonrails.org/action_controller_overview.html#strong-parameters

票数 1
EN

Stack Overflow用户

发布于 2016-06-13 20:26:12

在您的操作中,尝试将当前行替换为以下一行:

@ current_user.challenges.send(params:categorization).order("deadline ASC")

它应该能工作

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37798418

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档