How to elegant the first time show popup with Rails - ruby-on-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

Related

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.

How to trigger modal when event occurs in Rails

I'm sure that there is an answer to this out there, but I'm not entirely certain how to properly phrase this question, so my apologies if this is repetitious.
I am working on implementing a badge/achievement system for a site. The backend stuff is there, but I'm working on the front-end now, and I'm basically trying to figure out how to redirect to a sort of "Congratulations!" page when someone gets a new badge.
The congratulations page is going to be a modal, but for simplicities sake, if anyone has an idea of how to trigger an action like this only once when a new Badge is created, that would be a huge help. Right now, when a user performs a given action, say... adding money to their account, a user_badge is created (adds a Badge ID to an array).
Thanks in advance!
You can use before_filter for actions, where you want to check that new badge appears (probably you don't want to check it in auth actions, so you can't use global before_filter). Then you can use redirect_to in this before filter, if user has a new badge.
But I don't recommend to do so, since you'll break the users flow. It's better to show alert/modal on the next requested page. To achieve this you can define a new instance variable in your before_filter (like #show_modal) and include the partial with modal to your footer.
example:
#before filter
def check_for_new_badges
if current_user.has_new_badges?
#show_modal = true
current_user.set_badges_as_seen!
end
end
#included in layout
- if #show_modal
= render 'shared/congrats_modal'

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 to store where a new user was referred? Using Rails + Devise

I have a rails app that uses devise. I'm curious to know, is it possible in the User table to somehow track where a new user came from, the HTTP referrer?
I'd like to know which came from Facebook, Twitter, LinkedIn, Google+ in order to track a viral loop.
Any ideas? Seen anyone do this? Possible? Where should this live in the rails app? Still very new. Thanks
It could be done like this. May require some tweaking and fixing but You'll get an idea
Make before filter for Application controller, you will call it for any action
def landing_filter
if from_other_site(request.referrer) and !session[:referer].blank?
session[:referer] = request.referrer #you don't want to delete first entrance
end
end
from_other_site should be the method which will check domain name in referrer url, if it match your then return false, otherwise true
in devise/registration/new.erb.html view add in form hidden field
<%= f.hidden_field :referrer, session[:referrer] %>
and don't forget to add migration with new database field for user
Save referer somewhere and after creating a user copy information to user table. Using session to save referer works but permanent cookies are better. Cookies can persist the information even when user closes browser and comes again in the next day.
# so basically in ApplicationContreller using before_filter
def referer_before_filter
if cookies[:referer].blank?
cookies.permanent[:referer] = request.env["HTTP_REFERER"] || 'none'
end
end
# and in signup action somewhere else saving that information
#user.referer = cookies[:referer] # or maybe to some other table
Instead of modifying every action you can also use rails sweepers/observers to handle automatic saving every time an object is created.
A good gem to automatically save referer and other needed information is
https://github.com/holli/referer_tracking . You can choose do you want to save information manually or use sweepers to do saving automatically.

Ruby on Rails. How to show record only once?

I want to know how to show a record only exactly after it was created. So i want to show it only once.
Just see. I have model called Claim. User creates new Claim (user is anonymous). I want to show this Claim only ofter user creates it and never again. How can I do it?
I think that I can use flash hash, but i think it is not enough secure to create symbols from user data.
You could have a viewed attribute on your model that you set to true in the show action. If viewed is true then skip rendering the view.
unless thingy.viewed
thingy.viewed = true
thingy.save
render like normal...
end

Resources