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. 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.
The approach I’m trying is somehow showing/filtering? all posts where :poster matches the user id of the user’s page; 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 %>. 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:
@useris the the record for the user who wall is now being viewed. (right?) You need all the posts that were posted to that users wall that is all the Post records whereposter == @user.id. There is no need to look in theparamshash for this. I believe that this line should instead be:If this is incorrect, it means that I am not clear how your
showaction is meant to work. There are two different things that could be@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?