My page wouldn’t flash the user.
Trundling along the Hartl railstutorial, I needed to write some logic to allow a user to update their information.
As a loyal TDD-er, I wrote a test to check that users see a success message if they are able to update their infomation:
describe "updating with valid information" do
let(:new_name) { "New Name" }
let(:new_email) { "new@example.com" }
before do
fill_in "Name", with: new_name
fill_in "Email", with: new_email
fill_in "Password", with: user.password
fill_in "Confirm Password", with: user.password
click_button "Save changes"
end
it { should have_selector('div.alert.alert-success') }
end
As expected, the tests should fail until I am able to log in.
Since the Rails4 user scaffold comes with some nifty built in methods, I thought it would be easy to adapt them for my own ends:
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'Profile updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
I modify the line 5 of the user controller to change the “notice” message to a “success” message:
format.html { redirect_to @user, success: "Profile updated" }
Seems reasonable, but my test for the flash message is still failing. Why won’t my page flash the user?!
Quick as a flash, I asked Dash about FlashHash.
Lo, and behold! Notice and alert are built-in flash tags, but success is nowhere to be found. How can I express the satisfaction of SUCCESS!!
with a tag as mundane as notice:?
Thanks to duck-typing (if it walks like a duck and quacks like a duck, it is probably a hash.. right?) we can add our spiffy success:
tag to flash, and share the appropriate level of enthusiasm with our users:
format.html { redirect_to @user, flash: { success: "Profile updated" }}
All tests are green, and today I learned that flash is
Awesome