In my Ruby on Rails app, I would like to only load a modal when the user loads the page the first time.
I decided to use cookies and set cookie(user saw once the page) = true by setting it to this value after the page has been loaded.
This way, the user loads the page the first time, the views loads (cookie value still false), then the cookie is set to 'true' and so the second time he will load the page, it won't load the modal.
But it does not work. I think the cookie gets set before the view is loaded altough I have placed the code lines after respond_to , at the end of the contorller action showcase (see below).
controller 'deal'
def showcase # load the page
@deal = Deal.friendly.find(params[:id])
respond_to do |format|
format.html # showcase.html.erb
format.json { render json: @deal }
end
cookies.permanent[:modal_loaded_once] = true
end
view showcase.html.erb
//this will load the modal only once as the first time it is loaded the // cookie value is still 'false'
<% if @modal_loaded_once = false %>
<script>
$(window).load(function(){
$("a.modal:first").trigger("click");
});
</script>
<% end %>
EDIT: answer
$(window).load(function(){
if ($.cookie("show_modal_shownNX1") == null) {
$.cookie('show_modal_shownNX1', 'yes', { expires: 720, path: '/' });
$("a.modal:first").trigger("click");
}
});