我想知道是否可以在视图中指定顺序(即: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