Hi, I am logging users in and keeping them in session. The problem I am having is showing only a certain logged in user''s information. In the Adverts_Controler: def show @advert = Advert.find(params[:id]) end I am also having issues with saving an advert for an user: def create @advert = Advert.new(params[:advert]) if @advert.save flash[:notice] = ''Advert was successfully created.'' redirect_to :action => ''list'' else render :action => ''new'' end end How do I get the user_id from the session and use it in above functions to only show users''s adverts and to pass user_id to the create function to create an advert for the logged in user? -- Posted via http://www.ruby-forum.com/.
-- -- Tom Mornini On Apr 2, 2006, at 12:27 PM, david wrote:> Hi, > > I am logging users in and keeping them in session. The problem I am > having is showing only a certain logged in user''s information. > > In the Adverts_Controler: > > def show > @advert = Advert.find(params[:id]) > end > > I am also having issues with saving an advert for an user: > def create > @advert = Advert.new(params[:advert]) > if @advert.save > flash[:notice] = ''Advert was successfully created.'' > redirect_to :action => ''list'' > else > render :action => ''new'' > end > end > > How do I get the user_id from the session and use it in above > functions > to only show users''s adverts and to pass user_id to the create > function > to create an advert for the logged in user?def show @user = User.find(session[:user_id]) # Ideally, put this in application.rb and reference via before_filter @advert = @user.adverts.find(params[:id]) end def create @user = User.find(session[:user_id]) # Ideally, put this in application.rb and reference via before_filter @advert = Advert.new(params[:advert]) @user.adverts << Advert.new(params[:advert]) if @advert.save flash[:notice] = ''Advert was successfully created.'' redirect_to :action => ''list'' else render :action => ''new'' end end -- -- Tom Mornini
> def show > @user = User.find(session[:user_id]) # Ideally, put this in > application.rb and reference via before_filter > @advert = @user.adverts.find(params[:id]) > end > > def create > @user = User.find(session[:user_id]) # Ideally, put this in > application.rb and reference via before_filter > > @advert = Advert.new(params[:advert]) > > @user.adverts << Advert.new(params[:advert]) > > if @advert.save > flash[:notice] = ''Advert was successfully created.'' > redirect_to :action => ''list'' > else > render :action => ''new'' > end > end > > -- > -- Tom MorniniThanks for your help Tom. The create funtion creates double entries in the DB one correctly and the other it creates an entry with user_id 0, which is bad. The show function still shows all instead of filtering on user_id. Any ideas? -- Posted via http://www.ruby-forum.com/.
>> -- >> -- Tom Mornini > > Thanks for your help Tom. > > The create funtion creates double entries in the DB one correctly and > the other it creates an entry with user_id 0, which is bad. > > The show function still shows all instead of filtering on user_id. > > Any ideas?I answered my own question: @user = User.find(session[:user_id]) # Ideally, put this inapplication.rb and reference via before_filter @advert = Advert.new(params[:advert]) @user.adverts << @advert This code works. If you call new twice, then you are going to get 2 entries in the DB of course. I am still stuggeling with filtering the show function on user_id though. -- Posted via http://www.ruby-forum.com/.
Jeff Coleman
2006-Apr-03 01:46 UTC
[Rails] Re: Showing current user''s information and saving
david wrote:> > I am still stuggeling with filtering the show function on user_id > though.This code only finds a single advert. def show @user = User.find(session[:user_id]) # Ideally, put this in application.rb and reference via before_filter @advert = @user.adverts.find(params[:id]) end Use this to find all the adverts related to the current user: def show @user = User.find(session[:user_id]) # Ideally, put this in application.rb and reference via before_filter @adverts = @user.adverts end If the associations are correct in the model, you should always be able to find a user''s adverts with the "@user.adverts" method. Hope this helps, Jeff -- Posted via http://www.ruby-forum.com/.
Tom Mornini
2006-Apr-03 05:56 UTC
[Rails] Re: Showing current user''s information and saving
On Apr 2, 2006, at 5:22 PM, david wrote:>>> -- >>> -- Tom Mornini >> >> Thanks for your help Tom. >> >> The create funtion creates double entries in the DB one correctly and >> the other it creates an entry with user_id 0, which is bad. >> >> The show function still shows all instead of filtering on user_id. >> >> Any ideas? > > I answered my own question: > > @user = User.find(session[:user_id]) # Ideally, put this > inapplication.rb and reference via before_filter > @advert = Advert.new(params[:advert]) > @user.adverts << @advert > > This code works. > > If you call new twice, then you are going to get 2 entries in the > DB of > course.Yes, do what I meant, not what I said. :-) However, calling new shouldn''t create a row until the object is saved. -- -- Tom Mornini