Rails supports alert
and notice
as default flash types. This allows you to use these keys as options in e.g. redirect_to
and as a helper in views e.g. <%= notice %>
(instead of flash[:notice]
).
class SomeController < ApplicationRecord
def create
@user = User.create!
redirect_to user_path(@user), notice: "#{@user} created!"
end
end
In case you are using Bootstrap as CSS framework you might also want to support flashes like success
. This can be done with the add_flash_types
method.
class ApplicationController < ActionController::Base
add_flash_types :success
end
class SomeController < ApplicationRecord
def create
@user = User.create!
# Before adding success as flash type
# flash[:success] = "#{@user} created!"
# redirect_to user_path(@user)
# After adding success as flash type
redirect_to user_path(@user), success: "#{@user} created!"
end
end
Posted by Emanuel to makandra dev (2021-01-18 11:41)