Welcome flash message after sign up? - ruby-on-rails

When users sign up to my application I have a welcome message appear on the home page using Devise's sign_in_count column.
def home
if current_user.sign_in_count == 1
flash.now[:notice] = "Welcome!"
end
end
The only problem though is it stays there until they sign out and sign back in. How can I make it show only once and disappear when the page is refreshed or changed? Is there some rails way to do this?
Thank you.
EDIT
application.html.erb
<body>
<div class="container">
<%= render "shared/flash_message" %>
<%= yield %>
</div>
</body>
_flash_message.html.erb
<% [:notice, :error, :alert].each do |level| %>
<% unless flash[level].blank? %>
<div class="span12">
<div class="<%= flash_class(level) %> fade in">
×
<%= content_tag :p, flash[level] %>
</div>
</div>
<% end %>
<% end %>

In your layouts/application.html.erb you should have something like this:
<% flash.each do |key, value| %>
<%= content_tag(:div, value, class: "flash #{key}") %>
<% end %>
Doing this way, should works as you expect.
EDIT
What if you verify for the current_user is already set?
def home
if current_user && current_user.sign_in_count == 1
flash.now[:notice] = "Welcome!"
end
end
EDIT 2
OK! Got it! The sign_in_count column from Devise will remain the same until the next login, so, it will always keep showing you the Welcome! message. To make this work as you expect you have to create a flag on it.
def home
if current_user && current_user.sign_in_count == 1
unless session[:display_welcome]
flash.now[:notice] = "Welcome!"
session[:display_welcome] = true
end
end
end
You can try using session or cookies.

If the user logs in for the first time, change the logic to compare against 0. After setting flash message update the sign_in_count to 1,
def home
if current_user.sign_in_count == 0
flash.now[:notice] = "Welcome!"
current_user.update_attribute(:sign_in_count, 1)
end
end

Related

Display modal (only 1 time) after first login with Ruby on Rails + Devise

I'm trying to show an introduction modal to new user after the first sign in.
<% if user_signed_in? && current_user.sign_in_count == 1 %>
<%= render 'introduction_slides' %>
<% end %>
The sign_in_count works great. but the problem is that user needs to signout and in to stop seeing that modal. Is there any better way to make this happen? Maybe using Cookie?
I figured out with Session 👍
<% if user_signed_in? && current_user.sign_in_count == 1 %>
<% unless session[:introduction_slides] %>
<%= render 'introduction_slides' %>
<% session[:introduction_slides] = true %>
<% end %>
<% end %>

Rails flash message

I am building an application on Rails where a user creates a Test, goes to the show view for that test and fills in a form with an answer to a question. If the answer to the question matches the "correct_answer" (defined in the controller), there will be flash message that the answer was correct and a button to continue to the root_path will appear. If the answer is wrong, the flash says "Wrong answer."
My problem is that the flash message will say wrong answer even if no answer has been given. I ONLY want that message to appear AFTER the user has submitted the form. I understand why this is happening, I just am unsure about how to fix it. Here is the show view for a test:
<div class="col-md-8 col-md-push-2">
<h4>Current Score: <%= #test.score %></h4>
<br /><br />
<div class="form_group">
<%= form_tag test_path(#test), :method=> 'get' do %>
<h4>What is my first name?</h4>
<div class="form-group">
<%= text_field_tag :answer, params[:answer], class: 'form-control' %>
</div>
<% if !flash[:success] %>
<div class="form-group">
<%= submit_tag "Submit", class: "btn btn-primary" %>
</div>
<% end %>
<% end %>
</div>
<% if flash[:success] %>
<%= link_to "Continue", root_path, class: "btn btn-success pull-right" %>
<% end %>
</div>
Here is the controller for tests, which contains the offending show action:
class TestsController < ApplicationController
def index
#test = Test.new
#tests = Test.all
end
def show
#test = Test.find(params[:id])
correct_answer = "jack"
user_answer = params[:answer]
if user_answer == correct_answer
flash.now[:success] = "That is correct!"
new_score = #test.score += 1
#test.update(score: new_score)
elsif params[:answer] != correct_answer
flash.now[:danger] = "Wrong answer"
end
end
def create
#test = Test.create(test_params)
if #test.save
redirect_to test_path(#test)
flash[:success] = "Test created"
else
flash[:danger] = "There was a problem"
render "index"
end
end
def destroy
#test = Test.find(params[:id])
if #test.destroy
flash[:success] = "Your test was removed"
redirect_to root_path
end
end
private
def test_params
params.require(:test).permit(:score, :user_id)
end
end
Is there a better way to do this? If not, can I somehow stop the flash message from appearing on the initial load? I ONLY want it to appear once the form has been submitted. Thanks in advance.
So your flash is triggered because params[:answer] is undefined and not == to jack.
You say:
My problem is that the flash message will say wrong answer even if no answer has been given.
But your logic is off for this outcome, your code says that params[:answer] is defined, which it can't be since the form is not rendered yet.
Maybe your condition should be:
elsif params[:answer].present? && params[:answer] != correct_answer
flash.now[:danger] = "Wrong answer"
end
Traditionally, the showing of the form and POST to a form are separete actions, which is why you're seeing this flash before anything even happens.

Current_user && User profile

So in my To-do app I have profiles for each user that displays a list of all the Items they have created like so :
<% if current_user %>
<h1>Hello, <%= current_user.username.capitalize%>, </h1>
<% else %>
<h1><%= #user.username.capitalize%> To - Do List. </h1>
<%end %>
<h4> List of Items </h4>
<%= render :partial => 'items/form', :locals =>{:item => Item.new} %>
<% #user.items.order('created_at DESC').each do |item| %>
<%= render :partial => 'items/item' , :locals => {:item => item } %>
<% end %>
My problem is that I want users to be able view other Users profile but with the Header changing according to who is viewing it.
<h1>Hello, <%= current_user.username.capitalize%>, Here is your To-do Items</h1>
if its the current_users own profile or:
<h1><%= #user.username.capitalize%> To - Do List. </h1>
if the user is viewing another users profile.
As you can see I tried a little something with an if statement but it doesn't seem to be working as i want it to .Can anyone help me out please?
Try current_user == #user in if condition.
If there is a possibility that current_user could be nil, then
Try current_user.present? && current_user == #user in if condition.
In your controller you may have:
#user = User.find(params[:id])
Then in your view something along the line of
<% if current_user == #user %>
use this code:
current_user == #user if condition.

Flash[:notice] not working with Mercury Editor authentication

In my lib/mercury/authentication.rb file I have
module Mercury
module Authentication
def can_edit?
if user_signed_in? and current_user.role == 'admin'
true
else
#flash[:notice] = "You are not authorized to view that page."
redirect_to root_path, notice:"You are not authorized to view that page."
end
end
end
end
And app/views/layouts/application.html.erb
<% if notice %>
<p class="alert alert-success"><%= notice %></p>
<% end %>
<% if alert %>
<p class="alert alert-danger"><%= alert %></p>
<% end %>
Yet the 'notice' never appears, even though it is working with Devise!
Thanks for any input!

Rails .html.erb - if, else statement not working

I am trying to show something if a user has 'messages'
views/messages/index.html.erb
<% if #user.messages.any? %>
You have messages!
<% else %>
Sorry, you have no messages
<% end %>
controllers/messages_controller.rb
class MessagesController < ApplicationController
def index
#user = current_user
#messages = #user.messages
end
When I have this code implemented even though the user has no messages (as confirmed by rails console :
user = User.find(1)
user.messages.any?
=> false
The message 'You have messages!' still appears
Also thought to add that the authentication is using the devise gem
<% if #user.messages.any? %>
<%= "User with id #{#user.id} has messages" %>
<% else %>
<%= "User with id #{#user.id} doesnt have messages" %>
<% end %>
This will show something like
User with id 5 has messages
or
User with id 6 doesnt have messages
Now verify this from rails console
User.find(5).messages.any?
try
<% if #user.present? %>
<%= puts "User is selected" %>
<% if #user.messages.present? %>
<%= puts "User have message" %>
<% end %>
<% end %>

Resources