"User last online" at <time> using devise? - ruby-on-rails

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

Related

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

Rails - How do I refresh a page without reloading it?

Rails - How do I refresh a page without reloading it ?
I'm building an app which has a function that gives out a random number (random record) every time people go to that page, but the problem is that when user goes to that page, a random number is given but that number will change again if the user refresh the page.
How do keep that random number even when user refresh the page or comes back to that page, the random number stays the same.
I'm thinking disabling the refresh functionality in browsers which will stop users from refreshing the page hence stop them from changing the random number but after researching, it looks like it is a good idea to disabling the refresh functionality.
Is there any other methods to achieve it?
----Update----
I have tried
#posts_controller.rb
def new
#random = cookies[:stuff] ||= Stuff.random
end
and call #random.content in my view since I want only the content of the Stuff model, the first time is fine but when I refresh the page, it gives me undefined method 'content' for "#<Stuff:0x0000010331ac00>":String What's going on?
You could do this in your controller action:
# ex. app/controllers/index_controller.rb
def index
session[:random] ||= rand(10000)
end
This would generate a random number between 0 and 10000 and save it in the user's session, only if it wasn't already present before. This means that it will be the same for each specific user until he closes his browser.
If you want persist even after closing the browser then you could use cookies instead:
cookies[:random] ||= rand(10000)
You can replace the rand(10000) call by whatever custom method you want.

Show a Dialog When Users Login for the First Time

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] %>

How can I obtain user's last active time when using Devise?

Suppose Users are spending long time in the web-site.
How can I obtain user's last active time.
Last active time means the last timestamp of user who requested(accessed to the page. user even doesn't need to update or create new record) to app.
A User might have signed in about an hour ago. Then he's still web surfing in my web site.
I want the time of last request the user sent.
Is it possible?
Devise has current_sign_in_at but not last active time.
The way I would implement this is to add a last_active_at datetime column to the User model, then putting something in my controller:
class ApplicationController
before_filter :record_user_activity
private
def record_user_activity
if current_user
current_user.touch :last_active_at
end
end
end

Session problem using Facebooker with Ruby on Rails

I am reading the book Facebook Platform Development in order to try to code a small game for Facebook, and I have come across a "little" problem: I am trying to insert a user every time this is logged, into a database in my computer. I am using a couple of methods written in the book, but there seems to be a couple of problems:
I can't seem to retrieve the session_key from Facebook using the Facebooker helpers for RoR, and thus this value is null into the table in my database.
Every time I reload the webpage, I can see that even though the facebook_id is the same, the same user is added in another row to my table in the database, even though it shouldn't; it's just supposed to update the attribute session_key if this changes -anyway, right now this is null.
These are the three methods I am using in order to perform all this:
def self.for(facebook_id,facebook_session=nil)
user = User.find_or_create_by_facebook_id(facebook_id)
unless facebook_session.nil?
user.store_session(facebook_session.session_key)
end
end
def store_session(session_key)
if self.session_key != session_key
update_attribute(:session_key, session_key)
end
end
# Re-create a Facebooker::Session object outside a request
def facebook_session
#facebook_session ||= returning Facebooker::Session.create do |session|
# Facebook sessions are good for only one hour storing
session.secure_with!(session_key,facebook_id,1.hour.from_now)
end
end
Thanks a lot in advance to everybody!1.
Hey sadly facebook changes its API all the time!
Make sure that the book is up to date and that none of the API has changed as of when the book was written. Also check that the gem is also up to date.
I personally use http://github.com/chrisdinn/hyper-graph when dealing with facebook. It makes calls to the facebook graph (graph.facebook.com)

Resources