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?
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
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 M.
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.
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.
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.