0

我想在 Post 表中显示所有帖子,说 id=5 ......

控制器

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

看法

profile.html.erb

    <h1>In Profile</h1>
    <%=session[:test]%>
    <% @post.each do |p|%>
    <%= p.title%>
    <%= p.body%>
    <%= p.tag%> 
    <%end%>

当我执行时,我收到一个错误,例如....

请帮我修复错误......谢谢。

4

2 回答 2

1

Post.find_by_userid(uid)Post.where(:userid => uid).first,只返回一条记录。

你应该使用Post.where(:userid => uid)

于 2012-06-21T05:35:04.663 回答
0

您正在使用Post.find_by_user(uid)的不是您可以迭代的东西。它只返回一条记录。

使用返回集合的东西,比如Post.where(:userid => uid)

于 2012-06-21T05:34:10.230 回答