So the step I’m having trouble with (Step 2) reads as follows:
“Between the new action and the private method, add the create action”
The code it want’s me to add looks roughly* like this (*I added a bit more whitespace to make it look a little cleaner):
def create
@user = User.new(user_params)
if @user.save
session[:user_id] = @user.id
redirect_to '/'
else
redirect_to '/signup'
end
end
The problem I’m having is that for some reason I am unable to complete this step, and I don’t get any error messages.
For those that need it, my code is as follows:
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
session[:user_id] = @user.id
redirect_to '/'
else
redirect_to '/signup'
end
end
private
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :password)
end
end
Additional information that might be relevant:
I used the tab button to make my whitespace in my actual code for my formatting but, used spaces here.
If it might be my browser, I use google chrome, I have attempted to try and pass this step with safari as well but, might also be worth testing with safari if necessary.
So, I found out that this code would allow me to pass the step (Found it on the forums after making my post because, I didn’t see it previously even though I spent what I’ll call 2 hours (Might be more since it was spread out through different parts of 2 days for different amounts of time) searching for an answer, Link to forum post ):
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user =
User.new()
if @user.save
session[:user_id] =
@user.id
redirect_to '/'
else
redirect_to '/signup'
end
end
private
def user_params
params.require(:user).permit(
:first_name, :last_name,
:email, :password)
end
end
But, now I want to know why this code works and not mine, even though in my eyes they are identical. I also want to know why my code didn’t work even though the code I used is pretty much a copy/paste of the code I was told to use save for the way I formatted.
Note: This code is what it would look like after it’s corrected, the original code is on the linked forum post. Additional link so you don’t have to scroll back up to get it.
Perhaps copying the code from the way I formatted my code might break things, try pulling the code from the original post and move the 2nd end at the first def to the bottom.