Reputation: 27495
I have a model in which allows a user to create many of. I want only the user to see these objects if they are logged in. So as of now I have the object belongs_to:user
and user has_many:objects
set, but how do I set the user property when an object is created. I basically want the user to create this and then later on display all of them back using object find(where user=current_user)
.
Upvotes: 1
Views: 692
Reputation: 49713
In your objects_controller.rb create action do this:
@object = current_user.objects.build(params[:object])
Instead of:
@object = Object.new(params[:object])
To retrieve the current user's objects, simply:
@objects = current_user.objects
Upvotes: 5
Reputation: 4089
@object = current_user.objects.build(params[:object])
<% if current_user %>
<% objects = current_user.objects %>
<% for object in objects %>
<% object.name %>
<% end %>
<% else %>
<p>Not logged in</p>
<% end %>
Upvotes: 0
Reputation: 29155
You need something that will authenticate Users. I would suggest using Devise
or authlogic
Upvotes: 0