Reputation: 1515
User signs up, is redirected to a page to be collected info, pretty straight forward
I for my life can't figure out how to do this
My controller for the user
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def additional_info
#user = User.find session[:user_id]
@user = User.update(user_addinfo)
redirect_to user_path
end
def create
@user = User.new(user_params)
if @user.save
#session[:user_id] = @user.id
#UserMailer.welcome_email(@user).deliver
sign_in @user
redirect_to additional_info_path
flash[:success] = "Welcome to InYourShoes!"
else
render'new'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
def user_addinfo
params.require(:user).permit(:year)
end
end
user_addinfo
is the action method that i want to call updating my record on for my additional_info method.
the def create
method has commented line that i'm unsure if necessary, particularly the session[:user_id] = @user.id
. I was told that i need this in order to keep track of my session, but perhaps someone can debunk this for me, as im following michael hartl's tutorial.
as of right now with this code, rails is giving me a parameter missing in the
params.require(:user).permit(:year)
line.
Much help is greatly appreciated. Ive been trying many different things, and cant seem to figure this out
Upvotes: 0
Views: 66
Reputation: 53018
Change your controller code as below:
def additional_info
@user = User.find params[:id] ## Set @user
end
def update
if @user.update(user_addinfo)
redirect_to user_path(@user), notice: 'User was successfully updated.'
else
render action: 'additional_info'
end
end
def create
@user = User.new(user_params)
if @user.save
#session[:user_id] = @user.id
#UserMailer.welcome_email(@user).deliver
sign_in @user
redirect_to additional_info_path(@user) ## Pass @user
flash[:success] = "Welcome to InYourShoes!"
else
render'new'
end
end
and in your routes.rb
update the additional_info
route as
get 'info/:id' => 'users#additional_info', :as => 'additional_info'
Upvotes: 1
Reputation: 1398
The line you have commented in your create method:
#session[:user_id] = @user.id
Is what is storing the user id to a session variable and not a param in the url.
You then have this line commented in your additional_info method
#user = User.find session[:user_id]
This is looking up the user by the id that you would have previously stored in the session variable.
At that point the user object would be stored in user If you need it in your instance variable, make sure to modify the line to be
@user = User.find session[:user_id]
Your user would then be stored in @user and be able to be accessed in the view
Upvotes: 0
Reputation: 29349
You additional_info
action seems to be wrong. You need to pass in the id of the user for whom you are collecting additional information.
def additional_info
@user = User.find params[:id]
@user.update_attributes(user_addinfo)
redirect_to user_path(@user)
end
Upvotes: 0