I am a Rails newbie and I am trying to build a test site in which users
will have their own sets of data.
I understand how to manipulate shared data very well. Most of the Rails
examples out there seem to deal with such shared data. I am struggling
with the “best practice” method for a user to work with their own data
however (and not be able to view/edit other users’ data).
I am using LoginEngine and have the authentication working correctly.
I started by generating scaffolding for a model named “listing”. Each
user will have their own listings. The Listing model belongs_to user and
of course User has_many listings.
The scaffolding for ListingsController made this:
def list
@listing_pages, @listings = paginate :listings, :per_page => 10
end
Simple enough, but this shows all listings from any user of course. I’m
able to get it to work as I want by changing it to this:
def list
@listings = current_user.listings
@listing_pages = Paginator.new self, @listings.count, 10,
params[:page]
end
Is this the right way to restrict the data to the logged-in user (with
pagination)? It seems like I will be repeating myself quite often for
all the various user data. I have a feeling there is a simpler way. I
suppose I can reference current_user.listings directly in the View but
that does not give me pagination.
I tried to do pagination based on the scaffolding like this but it did
not work:
def list
@listing_pages, @listings = paginate :current_user.listings,
:per_page => 10
end
I thought this seemed logical but perhaps I am missing something
important about pagination or the Rails structure in general.
Thanks for any help or pointers to docs about this sort of thing!
Carl