Rails flashes are just name value pair hashes
$ rails console >> flash = { success: "It worked!", error: "It failed." } => {:success=>"It worked!", error: "It failed."}
To add them to your rails app go
app/views/layouts/application.html.erb
<html> <body> <%= render 'layouts/header' %> <div class="container"> <% flash.each do |key, value| %> <div class="alert alert-<%= key %>"><%= value %></div> <% end %> <%= yield %> <%= render 'layouts/footer' %> <%= debug(params) if Rails.env.development? %> </div> </body> </html>
This will produce
<div class="alert alert-success">Welcome to the Sample App!</div>
substituting the key value pair into the div. That’s why you can go flash[:error] and get a different style applied.
Then in your controller all you have to do is
controller.rb
class UsersController < ApplicationController def create @user = User.new(user_params) if @user.save flash[:success] = "Welcome to the Sample App!" redirect_to @user else render 'new' end end end
The nice styling you see comes via Twitter Bootstrap. Go here for a how to on that:
https://agilewarrior.wordpress.com/2014/03/06/how-to-create-a-new-rails-app-with-twitter-bootstrap/
links that help
http://ruby.railstutorial.org/book/ruby-on-rails-tutorial#sec-the_flash
Ruby on rails flash notice error - CSS PHP
Jan 13, 2016 @ 10:29:33
Nov 01, 2016 @ 09:57:33
Where is the code as indicated above, entered in the application? This is a little confusing.
$ rails console
>> flash = { success: “It worked!”, error: “It failed.” }
=> {:success=>”It worked!”, error: “It failed.”}
Dec 04, 2016 @ 23:52:34
Hi Nicholas. That code is just using the rails console. A kind of test bed for just typing in Ruby. It isn’t a part of the application. Just something I typed to show people how hashes work.
Dec 05, 2016 @ 00:18:16
Jan 27, 2017 @ 23:15:33
Very helpful, thank you!
Nov 10, 2017 @ 01:30:37
Was trying to implement but how to show a simplytoast message instead of notifications. simplytoast is a javascript non intrusive notifications that will fade away in x seconds
Jan 26, 2018 @ 16:53:45
Hi there.
I am wondering how can you provide line-number for your above code.
I really eager wait for your respond from you.
Thanks in advance.
Mar 19, 2019 @ 05:08:20