i dont know how to do it exactly, but imho there is a way to set a scope on
the AR model. when you set the scope to user_id = session[:user] (or sth
else) in eg a before_filter of that controller, you''ll only get
listings
which have user_id = session[:user], even when doing Listing.find(:all)
this way you can use the rails scaffold controller, add 1 line of code and
get what you want. isnt that great?
for more info on that, search the manual for AR
2006/8/15, Carl Johnson <carl_ivar@yahoo.com>:>
> 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
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
--
Michael Siebert <info@siebert-wd.de>
www.stellar-legends.de - Weltraum-Browsergame im Alpha-Stadium
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060815/0473457a/attachment.html