开发者

Showing Post + Poster's Info on the "Index" Page

I want to show a post along with the poster's info on my rails app. Right now I'm able to show the post and user association on the "show" page (the page for a single post), but when I want to show it on the "index" page (where all of my posts are), I get this error: undefined method `username' for nil:NilClass

I added @post.user = current_user to my post_controller under "show" (That allowed me to show the poster's info. But i dont know what to add to the "def index".

This is what I have there:

  def index
    @posts = Post.all
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @posts }
    end
  end

This is what I have under "show"

 def show
    @post = Post.find(params[:id])
    @post.user 开发者_运维百科= current_user
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @post }
    end
  end

I want to know what to add to my def index to be able to display the poster's info. Thanks in advance.


Your set up is wrong. The user needs to be added to the post when it is created and updated. As you have it now, all posts will always belong to the user that is currently viewing the post because you are assigning the current_user to the post in the show action (and would be doing the same in the index view if you continued with this approach) meaning that when post 1 is viewed by user 1 it would show as belonging to user 1. When post 1 is shown by user 2 it would belong to user 2.

The solution is to assign the current user to the post in the update and create actions of the posts controller exactly as you have it now in the show action but before the post gets physically updated or created.

Then remove the @post.user = current_user from the show action. This may mean you are left with legacy data in your app that doesn't have a user assigned. But that's o.k., just edit and save each post and it will have you attached automatically.

Then in the views/posts/index_html.erb just add the user details that you want to see in new table row columns before the show/edit links. This is assuming you have a standard scaffolded index.html.erb file. If you don't have a standard index view then put it wherever you want it but then if you had a customised index view you probably wouldn't be asking this question in the first place so forget about that and you'd know where it goes

Update

I did explain how to show the user in the views in my response above but possibly I need to be a little clearer so I'll show you the code. To show the user name in the show view use

<%= @post.user.name unless @product.user.blank? %>

To show the user in the index action for the post use

<% @posts.each do |post| %> <!-- This is already in your index action. -->
  <%=post.user.name unless post.user.blank?%><!-- This is the code you need -->
  <!-- You might want to add a new th in the header for the css table then you can add a <td></td> tags round the above line so your table all matches up nicely for display purposes-->
<%end%> <!-- This is already in your index action -->

Hope that helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜