yellowreign
yellowreign

Reputation: 3638

Flash Message Displaying Twice in Rails

I'm trying to display a flash when a user visits a page which thanks them for visiting the page which gives them the option to share the url with their friends. However, the flash message is showing on the page (correct), but then carrying over to the next page that they visit. How can I make the this only show on the first page?

Right now, the method/view where I want the message show is:

def show    
  user_message
end

user_message is a method in my app controller:

def user_message
  if current_user    
    flash[:page_visited] = "Thank you for visiting!"
  end
end  

I use a partial (that's included in my app layout) for my flash notices:

<% flash.each do |name, msg| %>
  <% if name == :notice %>  
    <div class="alert-message block-message success" data-alert="alert">
        <a class="close" data-dismiss="alert">×</a>
        <%= msg %>&nbsp;<br />  
    </div>
  <% elsif name == :page_visited %> 
    <div class="alert-message block_message warning" data-alert="alert">
        <a class="close" data-dismiss="alert">×</a>
        <strong><%= msg %><br><br></strong>
        Let your friends and family know!<br /><br />
        <!-- ShareThis Button BEGIN -->
        <span class='st_facebook_large'></span>
        <span class='st_twitter_large'></span>
        <span class='st_plusone_large'></span>      
        <span class='st_sharethis_large'></span>
        <!-- ShareThis Button END -->
    </div>  
  <% else %> 
    <div class="alert alert-message error" data-alert="alert">
        <a class="close" data-dismiss="alert">×</a>
        <%= msg %>
    </div>
  <% end %> 
<% end %>

So what happens is that it will show correctly on the :show view, but on the next page the user clicks to the message shows again and uses the flash "error" format and doesn't show the ShareThis links (although the url would not be the one I would want them to share anyway).

How can I correct this? I tried moving the user_message to a before_filter i.e.

before_filter :user_message, :only => :show

but that did the same thing - showed the flash message on :show and the subsequent page (as an error message).

Upvotes: 1

Views: 1082

Answers (1)

weakwire
weakwire

Reputation: 9300

try flash now

def user_message
  if current_user    
    flash.now[:page_visited] = "Thank you for visiting!"
  end
end  

Upvotes: 1

Related Questions