Reputation: 3422
I have a rails project where users
can create tournaments
.
I have this html page that takes to tournament#new
<div class="centre">
<a href="<%=new_tournament_path%>"><button class= "btn-success">AJOUTER VOTRE TOURNOI <br><img src="/assets/trophy.svg" alt="trophy"></button></a>
</div>
I have
in my tournament_controller
I have :
def new
if current_user.first_name.blank? || current_user.last_name.blank? || current_user.judge_number.blank? || current_user.telephone.blank? || current_user.birthdate.blank? || current_user.iban.blank? || current_user.bic.blank? || current_user.address.blank? || current_user.iban.blank? || current_user.bic.blank?
flash[:alert] = "Vous devez d'abord remplir <%= view_context.link_to 'votre profil', user_path(current_user) %> entièrement pour pouvoir ajouter votre tournoi"
redirect_to 'judge_connected'
elsif current_user.accepted == false
flash[:alert] = "Votre compte Juge Arbitre doit d'abord avoir été accepté par l'équipe WeTennis avant de pouvoir ajouter un tournoi. Assurez vous d'avoir bien rempli intégralement<%= view_context.link_to 'votre profil', user_path(current_user) %> afin d'avoir une réponse rapide."
redirect_to 'judge_connected'
else
create_mangopay_natural_user_and_wallet
create_mangopay_bank_account
@tournament = Tournament.new
authorize @tournament
end
end
When the user's profile is complete, it works fine and takes me to tournament/new.
When the two first conditions are fulfilled, it redirects correctly to judge_connected but never displays the flash alerts
Here is the code of my flashes in views/shared/flashes(_flashes.html.erb)
<% if notice %>
<div class="col-xs-offset-4 col-xs-6">
<div class="alert alert-info alert-dismissible marginflashes" id="alert" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<%= notice.html_safe %>
</div>
</div>
</div>
<% end%>
<% if alert %>
<div class="col-xs-offset-4 col-xs-6">
<div class="alert alert-warning alert-dismissible marginflashes" id="alert" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<%= alert.html_safe %>
</div>
</div>
<% end %>
I noticed that if I actually refresh the page after I click on my button. The flash is actually appearing ! But I want it to show up without having to refresh the page.
Any ideas ?
Upvotes: 2
Views: 441
Reputation: 1376
Very similar to Marius's answer, however what I personally like to do is the following...
add the following piece of code to your application.html.erb inside the body, so that this applies globally to all the views inside the application...
<% flash.each do |message_type, message| %>
<div class="alert alert-<%= message_type %>"><%= message %></div>
<% end %>
Elegant and simple.
Now, in regards to receiving flash messages instantly on the same page, you'll have to look into doing it with Ajax, since this kind of action should be done asynchronously. Personally, I prefer not to do it, but rather redirect to the main page of the app, for instance, where the message would be shown.
Hope this helps you out.
Upvotes: 1
Reputation: 1461
You need to add code in your view file to display the flash alert. Something like this (taken from the api documentation):
<% if flash[:alert] %>
<div class="alert"><%= flash[:alert] %></div>
<% end %>
You should probably put this in one of your layout files to keep it consistent across your application and to avoid code duplication.
Upvotes: 1