2012-04-08 5 views
0

친구들과 함께 게시 할 수있는 twiiter 앱을 만들었지 만 모든 게시물을 보여주는 목록에 해당 게시물을 만든 사람의 이름을 포함 시키려합니다. 여기 사용자 및 관련 게시물

이 협회 '사용자가 많은 게시물이'를 것을 물론, 가정

class PostsController < ApplicationController 
    def index 
    @posts = Post.all(:order => "created_at DESC") 

    respond_to do |format| 
     format.html 
    end 
    end 

    def create 
    @post = Post.create(:message => params[:message]) 

    respond_to do |format| 
     if @post.save 
     format.html { redirect_to posts_path } 
     format.js 
     else 
     flash[:notice] = "Message failed to save." 
     format.html { redirect_to posts_path } 
     end 
    end 
    end 
end 
` 

답변

1

이 설정되어 내 번호 및 사용자 모델은 '이름'필드가, 당신은 당신의보기에서이 작업을 수행 할 수 있습니다 : 당신이 말한 모든 것이가하는

<% @posts.each do |post| %> 
    <%= post.user.username %> 
<% end %> 
+0

이있다,하지만 난 전무의 오류 메시지'정의되지 않은 메서드'이름 '을 얻을 : NilClass' 난 후 컨트롤러 –

+0

아무것도 배치해야 할이 게시물이 부착되지 않음을 의미합니다 특정 사용자에게 연결되므로 연결이 없습니다. 오류가 발생하지 않도록 '<% = post.user.try (: username) %>' – Spyros

+0

'<% = div_for post do %> 행을 변경할 수 있습니다.

게시 됨 <% = time_ago_in_words (post.created_at) %> 전

<% = post.message %>

<% end %>' –

관련 문제