ruby on rails - Couldn't find User with 'id'=1003 -
i have 1000 tips , 95 users in postgresql database , want user able create new tip , taken show page can see tip name , description along author information.
however, getting error "couldn't find user 'id'=1003" raised on line @author_firstname = user.find(params[:id]).first_name
. i'm not sure why it's assigning user id new tip id, want user's id tied tip instead of tip id.
here tips controller:
class tipscontroller < applicationcontroller def index @tips = tip.all end def new @tip = tip.new end def create @tip = tip.create(tip_params) redirect_to "/tips/#{@tip.id}" end def show @tip = tip.find(params[:id]) @author_firstname = user.find(params[:id]).first_name @author_lastname = user.find(params[:id]).last_name end private def tip_params params.require(:tip).permit(:name, :description) end end
and of routes:
get '/users/new', to: 'users#new' post '/users', to: 'users#create' 'users/:id', to: 'users#show', as: 'user' 'users/:id/edit', to: 'users#edit' patch 'users/:id', to: 'users#update' '/places/:id', to: 'places#show' '/tips/new', to: 'tips#new' post '/tips', to: 'tips#create' '/tips/:id', to: 'tips#show', as: 'tip'
inside tipscontroller
, params[:id]
give id
of tip
not id
of user
.
as, tip
belongs_to user
, should able user
using this: @tip.user
so, show
method can be:
def show @tip = tip.find(params[:id]) @author_firstname = @tip.user.first_name @author_lastname = @tip.user.last_name end
Comments
Post a Comment