'create' action evaluates '@user.save', but where is this defined?


#1

5/12 of Rails Authentication 1 says to add the 'create' action to the users controller thus:

> def create 
>   @user = User.new(user_params) 
>   if @user.save 
>     session[:user_id] = @user.id 
>     redirect_to '/' 
>   else 
>     redirect_to '/signup' 
>   end 
> end

And this works and I can pass the exercise, however I do not understand and cannot see where '.save' for '@user' is defined. Is this a built in function, if so, what does it do?


#2

Hi @orourkea2017,

This is built in. If you open up your Rails console and create a new user there, at the end you have to type @user.save, or else it will be lost. When you save the user, it returns a value (true or false) to let you know whether or not the save was successful. The if statement there checks whether the user was successfully saved or not, then acts based on the result :slight_smile:


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.