i want to display all posts in Post table with say id=5 ……
controller
user_controller.rb
class UsersController < ApplicationController
# other methods are also present...
def profile
uid=session[:userid] #session contains userid,it is stored in uid....Eg:5
@post=Post.find_by_userid(uid) #Display all posts with userid=5 in Post table.
end
end
view
profile.html.erb
<h1>In Profile</h1>
<%=session[:test]%>
<% @post.each do |p|%>
<%= p.title%>
<%= p.body%>
<%= p.tag%>
<%end%>
when i execute i get an error like….
Showing /Users/Vineeth/QA4/app/views/users/profile.html.erb where line #3 raised:undefined method `each’ for #
Please help me fix the error……thanks.
Post.find_by_userid(uid)is same asPost.where(:userid => uid).first, return only one record.You should use
Post.where(:userid => uid)