Forum: Ruby on Rails Showing current user's information and saving

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5e197c8f7663929dd8c2d978f0f0d0eb?d=identicon&s=25 david (Guest)
on 2006-04-02 21:27
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?
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-04-02 21:46
(Received via mailing list)
--
-- Tom Mornini


On Apr 2, 2006, at 12:27 PM, david wrote:

>
>
> 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
5e197c8f7663929dd8c2d978f0f0d0eb?d=identicon&s=25 david (Guest)
on 2006-04-03 02:14
> 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

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?
5e197c8f7663929dd8c2d978f0f0d0eb?d=identicon&s=25 david (Guest)
on 2006-04-03 02:22
>> --
>> -- 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.
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-04-03 03:46
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
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-04-03 07:57
(Received via mailing list)
On Apr 2, 2006, at 5:22 PM, david wrote:

>> Any ideas?
> 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
This topic is locked and can not be replied to.