I am learning rails, finished ruby basics. Still confused with colon before and after in ruby.
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
This is a create method automatically generated by rails scaffold command.
and please explain whats going on with the code.




:colonandcolon:are both symbols in Ruby, a type of data (like strings, integers, etc).As this Stack Exchange answer says, symbols are immutable. The link explains it better than I would so I'll leave that for them.
In the context of Ruby and Rails, you won't have to know too much besides some syntax rules:
In previous versions of Ruby, the rocket or arrow syntax was the only way to have a symbol
keypoint to a symbolvalue. Now, you can do a short-hand version using the after colon syntaxcolon:.One thing to note is that if you ever want a hash with a key that is a string, you have to use the arrow/rocket syntax:
For more info about that code, I recommend reading the Rails guide section about scaffolding, which explains the code generated via scaffolding.