我想知道是否可以在視圖中指定順序(即:order =>'created_at DESC')。我意識到視圖中的邏輯並不理想,但我似乎在定位影響此輸出的位置時遇到了一些問題。排序。每個結果在視圖中
舉例來說,這裏是我的代碼:
<% @user.questions.each do |question| %>
<%= link_to_unless_current h (question.title), question %>
Created about <%= time_ago_in_words h(question.created_at) %> ago
Updated about <%= time_ago_in_words h(question.updated_at) %> ago
<%= link_to 'Edit', edit_question_path(question) %> |
<%= link_to 'Destroy', question, :confirm => 'Are you sure?', :method => :delete %>
<% end %>
在我QuestionsController我有以下指標作用,但它不影響從上面的代碼的輸出。
class QuestionsController < ApplicationController
def index
@questions = Question.all(:order => 'created_at DESC', :limit => 20)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @questions }
end
end
end
更新:對於改變@ user.questions到@questions我得到這個錯誤:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
更新2:我想我應該指出,這個代碼是在問題顯示視圖。 views/questions/show.html.erb.
編輯我的回答:] – 2010-05-19 18:57:18