2016-01-11 33 views
0

我創建了一個有多個類別(如體育,歷史,藝術等)的瑣事遊戲。在每個類別的比賽結束時,我只想爲該單個類別顯示結果的記分牌。從Rails控制器調用單個方法

通過指定的方法類,我現在僅成功地過濾了歷史成績,爲在這裏看到:

class ScoreboardController < ApplicationController 
    def index 
     @participations = Participation.where(finished: true, category: "history").order(score: :desc) 
    end 
end 

雖然那好,我現在需要讓這個記分板控制器爲歷史,運動和任何其他數量的類別迭代產生過濾後的一組分數,而它僅涵蓋當前的歷史分類。

要做到這一點,我試圖創建控制器內的第二種方法爲:

@participations = Participation.where(finished: true, category: "sports").order(score: :desk) 

不幸的是,這仍然只是沒有體育成績返回得分第一方法的歷史。

我覺得我做的兩件事情錯了:

1)我是不是通過增加額外的迭代到相同的控制器寫入方法不正確?

2)在的記分板視圖(而不是記分板控制器),我沒有正確調用控制器。

任何想法我錯了嗎?

附加信息:記分牌查看

<thead> 
    <tr> 
    <th>#</th> 
    <th>User</th> 
    <th>Category</th> 
    <th>Score</th> 
    </tr> 
</thead> 

<tbody> 
    <% @participations.each_with_index do |participation, index| %> 
    <tr> 
     <td>#<%= index + 1 %></td> 
     <td><%= participation.user.username %></td> 
     <td><%= participation.category %></td> 
     <td><%= participation.score %></td> 
    </tr> 
    <% end %> 
</tbody> 

+0

請發表您的控制器代碼,而不僅僅是網頁摘要 – Tim

+0

@Tim - 這是記分牌合作上面的ntroller代碼。您是否要求參考模型? – darkginger

回答

1

你應該改變你的ScoreboardController指數包含了不同類別的不同的實例變量。即:

ScoreboardController.rb

def index 
    @participations_score = Participation.where(finished: true, category: "history").order(score: :desc) 
    @participations_history = Participation.where(finished: true, category: "sports").order(score: :desc) 
    # other particpation categories 
end 

index.html.erb

<h1>History Scoreboard</h1> 

<thead> 
    <tr> 
    <th>#</th> 
    <th>User</th> 
    <th>Category</th> 
    <th>Score</th> 
    </tr> 
</thead> 

<tbody> 
    <% @participations_history.each do |participation_history| %> 
    <tr> 
     <td>#<%= participation_history.id %></td> 
     <td><%= participation_history.username %></td> 
     <td><%= participation_history.category %></td> 
     <td><%= participation_history.score %></td> 
    </tr> 
    <% end %> 
</tbody> 

<!--- Add more code for the rest of the categories ---> 

我強烈建議你刷上你的Ruby編程語言的知識,以及Ruby on Rails框架。你可以去Code academy

+0

更改索引似乎是最簡單的解決方案。我試着通過創建用於participaations_history的實例變量來嘗試它,但它使用代碼後在index.html文件中返回一個錯誤:'undefined method each_'。在記分板的html文件中是否需要額外的魔法來識別實例? – darkginger

+0

@darkginger,我更改了代碼以刪除語法錯誤。它應該是'@ participations_history.each'而不是'@ participations_history.each_' – apebeast

+0

這是有效的。接得好。謝謝您的幫助。 – darkginger

0

聽起來像是nested resources工作,具有singular resource爲:

#config/routes.rb 
resources :categories do 
    resource :scoreboard, module: :categories #-> url.com/categories/:category_id/scoreboard 
end 

#app/controllers/categories/scoreboards_controller.rb 
class Categories::ScoreboardsController < ApplicationController 
    def show 
     @results = Participation.where(finished: true, category: params[;category_id]).order(score: :desc) 
    end 
end 

這將允許您調用scoreboards#show行動,從路線適當category填充....

#app/controllers/categories_controller.rb 
class CategoriesController < ApplicationController 
    def create 
     @category = ... 
     redirect_to [@category, :scoreboard] 
    end 
end 

協會

現在,有一種更好的方式。

而不是調用Participation赤裸裸的,你最好使用你應該已在後端設置了categoriesscoreboards協會:

#app/models/category.rb 
class Category < ActiveRecord::Base 
    has_many :participations 
    has_many :scoreboards, through: :participations 
end 

#app/models/participation.rb 
class Participation < ActiveRecord::Base 
    belongs_to :category 
    belongs_to :scoreboard 
end 

#app/models/scoreboard.rb 
class Category < ActiveRecord::Base 
    has_many :participations 
    has_many :categories, through: :participations 
end 

這將允許您使用與上述相同的路線,但這次填充通過協會迴應:

#app/controllers/categories/scoreboards_controller.rb 
class Categories::ScoreboardsController < ApplicationController 
    def show 
     @category = Category.find params[:id] 
     @results = @category.participations.where(participation: {attribute: true}) 
    end 
end 
相關問題