Sergioussss Sergioussss - 2 months ago 13
Ruby Question

Ruby on Rails Sign up error if @user.save

I'm trying to create a simple app to learn ruby, and while creating the sign up form an error appears:


NoMethodError in UsersController#create
undefined method `limit' for nil:NilClass


def create
@user = User.new(user_params)
if @user.save
flash.now[:noticia] = "#{user.nombre} se ha registrado correctamente"
redirect_to root_path
else


It says that the error stays at if @user.save

The whole code is:

def sign_up
@user = User.new
end

def create
@user = User.new(user_params)
if @user.save
flash.now[:noticia] = "#{user.nombre} se ha registrado correctamente"
redirect_to root_path
else
render 'sign_up'
end
end

def user_params
params.require(:user).permit(:nombre, :email, :password, :confirm_password)
end
end


Which is the problem?

Thank you!

Answer

The error says undefined method for nil:NilClass so the user query is not finding a user. Run the User.new query in the console which should help you understand why that query is not working. Have you set up the user_params correctly to reflect the user attribute names? For example should confirm_password be password_confirmation?

Comments