Reputation: 239
I created an twiiter like app, where friends can post, but i want a include the name of the person who created the post in the list showing all the post.
here is my code
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
`
Upvotes: 0
Views: 164
Reputation: 48626
Assuming, of course, that the 'user has many posts' association is set, and the user model has a 'username' field, you can do this in your view :
<% @posts.each do |post| %>
<%= post.user.username %>
<% end %>
Upvotes: 1