Show a Dialog When Users Login for the First Time - ruby-on-rails

I'm currently showing users a dialog if they're not following anyone within my app, but I only want to show this whenever it's the users first time logging in? Whenever a new users logs in for the first time, they're not following anyone, so their feed doesn't have any posts, so it shows the dialog. I'm achieving that using the following code:
<% if #posts.any? %>
I'm wanting to show a dialog on the users first login, and then it never appear again. I know Deivse has a sign_in_count option, but the dialog would stay there until a users logs out and logs back in.

If you add the :trackable module to your Devise setup, you'll get a last_sign_in_at and sign_in_count. Either of these should provide you with more than enough information to know when/if a user previously signed in. And posts.any? should probably go away.
For instance:
display_dialog unless current_user.last_sign_in_at.present?
or
display_dialog unless current_user.sign_in_count > 0

in controller
if #posts.blank? && user.sign_in_count == 1 # no posts and 1st time login
session[:display_dialog] = true
ens
in view
<%= display_dialog if session[:display_dialog] %>

Related

"User last online" at <time> using devise?

Is there any easy/native way to display when a user was last online on their profile page using a standard rails app using devise for authentication?
The closest I can get is
<%= #user.current_sign_in_at.to_s %>
This shows the last time a sign in occurred (which could differ significantly from the last time the user checked the site - i.e the last time they were 'online' so to speak). For example, I'd like to show a little green light if the user was using the site anytime in the last 90 seconds, but that won't be accurate if using the time of their last sign in.
Add a column in a users table last_seen_at & update it every time using touch.
class ApplicationController
before_action :record_last_seen_at
private
def record_last_seen_at
if current_user
current_user.touch :last_seen_at
end
end
end

How to elegant the first time show popup with Rails

I have to show user a popup for the first time they come to my site.
I did it that way,
When the user comes in the first time, the cookies has nothing,
so I will show popup, and save a flag to cookie.
The next time user comes, the popup won't show.
However, I think my implementation smells bad, seems not in a Rails way.
How can I improve it ?
View
- unless #has_shown_price_hint
:javascript
$("#hint_for_price").fadeToggle(2000);
document.cookie="has_shown_hint_for_price=true";
Controller
def index
#has_shown_price_hint = (cookies['has_shown_hint_for_price'].nil?)? false : true
end
In my opinion you can add login counter to user. It's a great idea by itself to know how loyal your users are.
Then checking if the user is the first-time visitor is just checking condition:
def first_time_visitor?
login_count == 0
end

Increment a users login count

I have an existing application where a user can log in etc. This does not use devise works fine. I wanted to create a feature that counts the users login attempts and as such increments this by one each time they log in. I am aware of active record and increment as i have put below.
def increment_login_count!
update_attribute(:login_count, login_count + 1)
end
Could anyone offer any other advice as to how to do this logic. I wanted to start with a controller spec and work from this but i am a little unsure.
Try this
def increment_login_count
increment! :login_count
end
And you can call this method whenever a new session is created for the user
You can refer to docs for more information

Sorcery redirects wrong

User wants to sign up at domain.com/users/new, however the user accidentally presses "enter" and all fields are filled in blank.
Now comes the problem. I have two options. If I remove...
...else
redirect_to new_user_path
end
the user will automatically be redirected to domain.com/users (after filling the fields in blank) and where the sign up field errors will nicely be displayed above the form.
However if I add that code above instead of removing it, the user will be redirected to the same link - that means /users/new and unfortunately the user will not have his form errors displayed nicely, they will actually not be displayed at all.
How come that the errors show up at domain.com/users and not at domain.com/users/new?

Rails 3 + Devise: user_signed_in? for a different user in the database?

I'm building an app where I want to add an online status for a given user.
I know that Devise has a method user_signed_in? built in to check if the user who is using the app is signed in or not. But when I try to use it for a different user like this:
user_signed_in?(user)
user.user_signed_in?
I obviously get an undefined method error.
Does Devise have a method for this or do I have to write my own?
One approach was to store the online status of a given user in the user model.
What's the best solution to this?
I have used Devise on my applications and experienced some of the same problems as you when I first began working with it. You are merely using the helper methods incorrectly. If you'd like to check if the current user has a session and is signed in, you use the helper as such:
if user_signed_in?
which is essentially the same statement as:
if !current_user.nil? && current_user.signed_in
If you'd like to check if a user object is signed in, then you call this: (where user is a User Model Object)
if user.signed_in?
I'm not the author of Devise, but from what I can tell of Warden / Devise neither keep track of who is logged in.
The problem with having an is_online column in the User table is that it is difficult to see who is active on the website. I would add a column to your User model called last_seen as a date-time, and update that with Devise every time the user requests a page. You could then easily add a User.online_count method or also see if a user has been seen at the website in the last 5 minutes.
Use devise_for :user in your routes.rb.

Resources