Reputation: 9674
I have a profile view where it lists all questions/blog posts that was submitted by the user. In the other views the order("created_at desc")
works properly without any bugs whatsoever. However, in my profile view even though I wrote @questions = Question.all.order("created_at desc")
It still displays them incorrectly. To exemplify the issue even more here's an image of what's currently happening.
As you can see, since I defined that questions should be displayed in descending order, shouldn't New test title number be displayed first?
My home controller:
class HomeController < ApplicationController
def index
@questions = Question.all.order("created_at desc")
@blogs = Blog.all.order("created_at desc")
end
def profile
@user = User.find(params[:id])
@answer = Answer.all.order("created_at")
@questions = Question.all.order('created_at DESC')
@blogs = Blog.all.order("created_at desc")
end
end
The loop I am using in the profile view:
<% @user.questions.each do |question| %>
<%= link_to question.title, question_path(question), :class => "ques" %>
<p class="pull-right">
<%= link_to question.user.username, question.user, :class => "bg" %>
</p>
<hr/>
<% end %>
P.S: @blogs
does the same thing as well.
Upvotes: 1
Views: 1127
Reputation: 33542
The problem is you have defined @questions = Question.all.order('created_at DESC')
in the profile
method but you are not using it in the view (you have @user.questions
in the view which isn't the same as @questions
). Also @questions
gives you all the questions not the particular user questions. You should change it to below
@questions = @user.questions.order('created_at DESC')
And in the view
<% @questions.each do |question| %>
<%= link_to question.title, question_path(question), :class => "ques" %>
<p class="pull-right">
<%= link_to question.user.username, question.user, :class => "bg" %>
</p>
<hr/>
<% end %>
Upvotes: 2