2016-06-13 14 views
1
<%= 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僅在控制器動作中顯示具有相同屬性的對象?

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

此時如果上述link_to用戶點擊我想只顯示與categorization: adventure屬性的挑戰。

我需要在pages#home中輸入什麼才能使這項工作成功?

pages_controller.rb

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

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

回答

1

How to only show one of the :categorization challenges if URL is root_path/:categorization?

您的默認@challenges已經返回有序ActiveRecord::Relation對象。因此,您可以將scope鏈接到它。

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

0

在你的行動,試圖通過這個替換當前行:

@challenges = current_user.challenges.send( params [:categorization])。order(「deadline ASC」)

它應該工作

+0

「無不是一個符號」 是一個錯誤 –

+0

你可以發佈 logger.debug的輸出PARAMS 到這一行動? –

+0

'PG :: UndefinedColumn:錯誤:列挑戰..categorizations不存在 線1:...挑戰「在哪裏挑戰」。「user_id」= $ 1和「挑戰...」 –

相關問題