Zastanawiam się, czy można dyktować zamówienie (tj .: order => 'created_at DESC') w widoku. Zdaję sobie sprawę, że logika w widoku nie jest idealna, ale wydaje mi się, że mam problemy z ustaleniem, gdzie wpływać na ten wynik.Zamawianie .each wyniki w widoku
Na przykład, tutaj jest mój kod:
<% @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 %>
W moim QuestionsController mam następujące działania indeksu, ale to nie wpływa na wyjście z kodem powyżej.
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
UPDATE: W odniesieniu do zmiany @ user.questions do @questions otrzymuję ten błąd:
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
UPDATE 2: Chyba powinienem wspomnieć, że ten kod jest w pytaniach pokazują widok . views/questions/show.html.erb.
Zmieniono moją odpowiedź:] –