简体   繁体   English

如何在用户页面上显示“墙贴”?

[英]How do I display “wall posts” on a user's page?

I'm trying to show "wall posts" that all users have made on a specific user's page, but I'm having difficulty showing the correct posts. 我试图显示所有用户在特定用户页面上所做的“墙贴”,但是我很难显示正确的贴子。

Users controller: 用户控制器:

def show
  @user = User.find_by_cached_slug(params[:id])
  @posts = Post.find_all_by_poster(params[@user.id])
  if signed_in?
    @post = Post.new
  end

  respond_to do |format|
    format.html # show.html.erb
    format.xml { render :xml => @user }
  end
end

Post form: 邮寄表格:

<%= form_for @post do |f| %>
  <div class="field">
    <%= f.text_area :content %>
  </div>
  <div class="field">
    <%= f.hidden_field :user_id, :value => current_user.id %>
  </div>
  <div class="field">
    <%= f.hidden_field :poster, :value => @user.id %.
  </div>
  <div class="actions">
    <%= f.submit "Submit" %>
  </div>
<% end %>

When a user creates a post the current_user id is saved in the posts table as the :user_id and the user id of the user's page they're posting on is saved as :poster . 当用户创建帖子时,current_user ID被保存为posts表中的:user_id ,而用户要发布的页面的用户ID被保存为:poster This part is working, but I don't understand how to render @posts so that only the posts that have been made on that user's page appear. 这部分工作正常,但是我不理解如何呈现@posts以便仅显示在该用户页面上发布的帖子。

The approach I'm trying is somehow showing/filtering? 我正在尝试的方法以某种方式显示/过滤? all posts where :poster matches the user id of the user's page; :poster匹配用户页面的用户ID的所有帖子; however, I don't know how to make this work or if there is a better way. 但是,我不知道如何进行这项工作,或者是否有更好的方法。 Any help? 有什么帮助吗?

Note: I'm using the slugged gem. 注意:我使用的是沉重的宝石。 In the Users/show view, I use <%= render @posts %> . 在“用户/显示”视图中,我使用<%= render @posts %> Eventually, I'd like for users to be able to comment on posts, if that affects any design decisions. 最终,如果影响任何设计决策,我希望用户能够对帖子发表评论。

Thanks very much for your help! 非常感谢您的帮助! Please let me know if any more information is needed. 请让我知道是否需要更多信息。

I think this line is the issue: 我认为这是问题所在:

@posts = Post.find_all_by_poster(params[@user.id])

@user is the the record for the user who wall is now being viewed. @user是正在查看墙的用户的记录。 (right?) You need all the posts that were posted to that users wall that is all the Post records where poster == @user.id . (对吗?)您需要所有已发布到该用户墙上的帖子,这是所有post记录,其中poster == @user.id There is no need to look in the params hash for this. 不需要为此查看params哈希。 I believe that this line should instead be: 我相信这行应该改为:

@posts = Post.find_all_by_poster(@user.id)

If this is incorrect, it means that I am not clear how your show action is meant to work. 如果不正确,则表示我不清楚您的show动作是如何工作的。 There are two different things that could be @user . @user可能有两种不同的情况。 By convention this should always refer to the record of the person at the other end of the connection. 按照惯例,这应始终引用连接另一端的人的记录。 In your case, it looks like it instead refers to the person whose wall is being viewed. 在您的情况下,它看起来是指正在查看其墙的人。 Is that correct? 那是对的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM