views:

15

answers:

2

I have post and user model and I have Many(posts) to One(User) association. I want to display only the posts which are created by that user(current user). So somehow I have to inject the currently logged in user's id into the "user_id" foreign key of the post model during creation. I'm using Devise as my authentication system. Any solutions of how to achieve this ? I'm using Rails version 3.0.

Thanks in advance.

A: 

With devise simply use the "current_user" helper ;)

<% if user_signed_in? %> 
  <% current_user.posts.each do |post| %> 
    <%= post.title %>
  <% end %>
<% end %>
ipsum
He cannot do this yet because the Post models don't know which user has made them.
Samuel
A: 

In your PostsController#create method, you can set the Post's user to the current_user (since you are using Devise).

def create
  @post = Post.create(params[:post]) do |p|
    p.user = current_user
  end
  #respond_with(@post) or Rails 2 equivalent
end
Samuel