AnthonyGalli.com AnthonyGalli.com - 1 month ago 15
Ruby Question

If link_to is clicked flash message on new page?

view

<% if current_user.challenges.badges.count > 0 %>
<%= link_to new_duel_path(:challenge_daddy => @user.id), class: "btn", style: "padding-top: 10px; padding-bottom: 10px; margin-top: 15px; color: white;" do %>
<span class='glyphicon glyphicon-tower'></span> Duel
<% end %>
<% else %>
# user will be sent to performance page
<%= link_to performance_path, class: "btn", style: "padding-top: 10px; padding-bottom: 10px; margin-top: 15px; color: white;" do %>
<span class='glyphicon glyphicon-tower'></span> Duel
<% end %>
<% end %>


controller

def performance
@user = current_user
# only if the user is brought to performance via the above link_to should this be flashed
flash[:alert] = "YOU'RE NOT AUTHORIZED TO INITIATE A DUEL UNTIL YOU BECOME A NINJA, BUT YOU CAN BE INVITED TO A DUEL"
end


I tried to send the flash directly in
link_to
, but nothing happened:
link_to performance_path(:alert => "Flash message")
also tried
(:error => "Flash message")

Answer

view

<%= link_to performance_path(alert: true), class: "btn", style: "padding-top: 10px; padding-bottom: 10px; margin-top: 15px; color: white;" do %>
    <span class='glyphicon glyphicon-tower'></span> Duel
<% end %>

controller

def performance
  @user = current_user
  flash[:alert] = "YOU'RE NOT AUTHORIZED TO INITIATE A DUEL UNTIL YOU BECOME A NINJA, BUT YOU CAN BE INVITED TO A DUEL" if params[:alert].present?
end

performance view

<% if flash[:alert] %>
    <%= flash[:alert] %>
<% end %>
Comments