def add_to_cart
# do some stuff
redirect_to_index
end
def empty_cart
# do some stuff
redirect_to_index("your cart was emptied")
end
def redirect_to_index(msg = nil)
flash[:notice] = msg if msg
redirect_to :action => :index
end
How come the flash notice works if I'm setting msg = nil? For example,
if I'm in empty_cart and pass "your cart was emptied" into
redirect_to_index, it passes through OK. I thought that setting
something equal to nil would remove the value. I'm confused.
def add_to_cart
# do some stuff
redirect_to_index
end
def empty_cart
# do some stuff
redirect_to_index("your cart was emptied")
end
def redirect_to_index(msg = nil)
flash[:notice] = msg if msg
redirect_to :action => :index
end
How come the flash notice works if I'm setting msg = nil? For example,
if I'm in empty_cart and pass "your cart was emptied" into
redirect_to_index, it passes through OK. I thought that setting
something equal to nil would remove the value. I'm confused.
What you've got there is a default value; it means: Set msg to nil if
and only if there's no argument supplied when the method is called.
Here's an example:
def say_word(word = "hello")
puts "#{word}!"
end
say_word # word will be "hello" (the default value)
say_word("goodbye") # word will be "goodbye"