Ruby on rails 如何显示当前用户';s帖子和所有帖子的评论?
我有很多用户,如何只列出一个用户的帖子,同时显示其他用户的所有评论 后模型Ruby on rails 如何显示当前用户';s帖子和所有帖子的评论?,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我有很多用户,如何只列出一个用户的帖子,同时显示其他用户的所有评论 后模型 belongs_to :user has_many :comments 注释模型 belongs_to :post belongs_to :user 用户模型 has_many :posts has_many :comments routes.rb resources :posts do resources :comments end 在控制器中 def index @post =
belongs_to :user
has_many :comments
注释模型
belongs_to :post
belongs_to :user
用户模型
has_many :posts
has_many :comments
routes.rb
resources :posts do
resources :comments
end
在控制器中
def index
@post = User.posts.all
@comment = User.comments.all
end
在视图index.html.erb中
<%@post.each do |post|%>
<%=post.post_name %>
<%= post.post_description %>
<%end%>
<%@comment.each do |comment|%>
<%=comment.content %>
<%end%>
<% @users.each do |user| %>
<% user.posts.each do |post|%>
<%= post.post_name %>
<%= post.post_description %>
<% post.comments.each do |comment| %>
<%= comment.content %>
<% end %>
<% end %>
<% end %>
<% current_user.posts.each do |post|%>
<%= post.post_name %>
<%= post.post_description %>
<% post.comments.each do |comment| %>
<%= comment.content %>
<% end %>
<% end %>
如何仅显示当前用户的帖子和评论?选项#1
<% @users.each do |user| %>
<% user.posts.each do |post| %>
<%= post.post_name %>
<%= post.post_description %>
<% end %>
<% user.comments.each do |comment| %>
<%= comment.content %>
<% end %>
<% end %>
#but I think that's not what you want
<% current_user.posts.each do |post| %>
<%= post.post_name %>
<%= post.post_description %>
<% end %>
<% current_user.comments.each do |comment| %>
<%= comment.content %>
<% end %>
您的用户模型有许多:posts
和Post有许多:comments
,因此您可以在控制器中这样做:
def index
@users = User.all
end
def index
@users = User.all
end
并通过用户调用每个帖子,通过帖子发表评论:
index.html.erb
<%@post.each do |post|%>
<%=post.post_name %>
<%= post.post_description %>
<%end%>
<%@comment.each do |comment|%>
<%=comment.content %>
<%end%>
<% @users.each do |user| %>
<% user.posts.each do |post|%>
<%= post.post_name %>
<%= post.post_description %>
<% post.comments.each do |comment| %>
<%= comment.content %>
<% end %>
<% end %>
<% end %>
<% current_user.posts.each do |post|%>
<%= post.post_name %>
<%= post.post_description %>
<% post.comments.each do |comment| %>
<%= comment.content %>
<% end %>
<% end %>
在你看来:
视图#1
<% @users.each do |user| %>
<% user.posts.each do |post| %>
<%= post.post_name %>
<%= post.post_description %>
<% end %>
<% user.comments.each do |comment| %>
<%= comment.content %>
<% end %>
<% end %>
#but I think that's not what you want
<% current_user.posts.each do |post| %>
<%= post.post_name %>
<%= post.post_description %>
<% end %>
<% current_user.comments.each do |comment| %>
<%= comment.content %>
<% end %>
我会尽力帮你,让我们先澄清你的问题,好吗?是否仅显示用户创建的帖子,是否仅向帖子所有者显示帖子以及所有评论?还是要列出所有帖子,以及帖子中的评论?如果没有,你能举个例子吗? 以阿拉拉坦为例,什么东西与你想要的东西不符? 您可以尝试以下方法: 您的用户\u controller.rb
def index
@users = User.all
end
def index
@posts= Post.all
end
def index
@comments = Comment.all
end
您的帖子。\u controller.rb
def index
@users = User.all
end
def index
@posts= Post.all
end
def index
@comments = Comment.all
end
您的评论\u controller.rb
def index
@users = User.all
end
def index
@posts= Post.all
end
def index
@comments = Comment.all
end
在你看来,这是一篇文章。
你可以把这个放进去
<% current_user.posts.each |post| %>
#post fields like
# <%= post.title %>
<% post.comments.each do |comment| %>
#comments fields like
# <%= comment.description %>
#do you wanna show the author of comment?
<% end %>
#or, do you wanna show the author of post?
<% end %>
#from what I understand you want to show both, that?
并通过用户调用每个帖子,通过帖子发表评论:
index.html.erb
<%@post.each do |post|%>
<%=post.post_name %>
<%= post.post_description %>
<%end%>
<%@comment.each do |comment|%>
<%=comment.content %>
<%end%>
<% @users.each do |user| %>
<% user.posts.each do |post|%>
<%= post.post_name %>
<%= post.post_description %>
<% post.comments.each do |comment| %>
<%= comment.content %>
<% end %>
<% end %>
<% end %>
<% current_user.posts.each do |post|%>
<%= post.post_name %>
<%= post.post_description %>
<% post.comments.each do |comment| %>
<%= comment.content %>
<% end %>
<% end %>
在你看来:
视图#1
<% @users.each do |user| %>
<% user.posts.each do |post| %>
<%= post.post_name %>
<%= post.post_description %>
<% end %>
<% user.comments.each do |comment| %>
<%= comment.content %>
<% end %>
<% end %>
#but I think that's not what you want
<% current_user.posts.each do |post| %>
<%= post.post_name %>
<%= post.post_description %>
<% end %>
<% current_user.comments.each do |comment| %>
<%= comment.content %>
<% end %>
但我想那不是你想要的
视图#2:
<% @users.each do |user| %>
<% user.posts.each do |post| %>
<%= post.post_name %>
<%= post.post_description %>
<% post.comments.each do |comment| %>
<%= comment.content %>
<% end %>
<% end %>
<% end %>
#I think this is what you want
<% current_user.posts.each do |post| %>
<%= post.post_name %>
<%= post.post_description %>
<% post.comments.each do |comment| %>
<%= comment.content %>
<% end %>
<% end %>
#我想这就是你想要的
为了访问特定用户的帖子,您必须实例化该用户,如user.first.posts
,否则您将帖子应用于用户模型,Rails认为这是一种方法。我不是geeting@SebastianPalma您必须实例化该用户
,这是正确的。请看下面我的答案。希望它能帮助你。它显示相同的ans所有用户的帖子和评论,我想显示帖子和评论所属的user@adarsh我们是来帮你的。但你的问题很不清楚(因此我猜是投了反对票)。因此,请慢慢澄清你的问题。解释您正在尝试执行的操作。您可以在包含中添加一个说明,以避免创建其他查询。它显示所有用户的帖子和评论,我不想显示帖子和评论所属的内容user@adarsh,第二个选项使用includes可以做你想做的事。我使用@users=User.includes(:posts,:comments)这将显示所有用户数据。我想显示的数据只属于用户帖子和评论数据好的,@adarsh,我创建了视图#2
。我想在我刚才回答的所有选项或代码中,你可以构造你想要的东西。我觉得您需要对RubyonRails进行基础研究。但我希望我能帮助你进行最新的编辑。你的userA会是当前用户吗?就像那样,我只想向userA显示userA的所有帖子和评论。你可以试试我输入的内容,如果或多或少是这样的话可以说吗?请停止缩短术语,比如请注意,我的英语不是很好。你明白我的问题了吗