Note that there are some explanatory texts on larger screens.

plurals
  1. POGradual engagement, persistent guest user with Devise
    text
    copied!<p>I'm trying to set up gradual engagement in my utility app which people can use without registering e.g. notepad.cc and jsfiddle.net and I plan to create a guest user (with Devise) for the user when he 'writes' to the app.</p> <p>I found this guide on the Devise wiki <a href="https://github.com/plataformatec/devise/wiki/How-To:-Create-a-guest-user" rel="noreferrer">https://github.com/plataformatec/devise/wiki/How-To:-Create-a-guest-user</a> which shows how to create a guest user for the duration of the browser session. What I want is for the user to continue using the same guest account in subsequent visits, until he signs up, maybe when I introduce subscription plans for more features.</p> <p>How can I modify what's in the guide to make this possible?</p> <p>Code in the guide linked above:</p> <pre><code># file: app/controllers/application_controller.rb class ApplicationController &lt; ActionController::Base protect_from_forgery # if user is logged in, return current_user, else return guest_user def current_or_guest_user if current_user if session[:guest_user_id] logging_in guest_user.destroy session[:guest_user_id] = nil end current_user else guest_user end end # find guest_user object associated with the current session, # creating one as needed def guest_user User.find(session[:guest_user_id].nil? ? session[:guest_user_id] = create_guest_user.id : session[:guest_user_id]) end # called (once) when the user logs in, insert any code your application needs # to hand off from guest_user to current_user. def logging_in end private def create_guest_user u = User.create(:name =&gt; "guest", :email =&gt; "guest_#{Time.now.to_i}#{rand(99)}@email_address.com") u.save(false) u end end </code></pre> <p>And using it in the controller:</p> <pre><code>@thing.user = current_or_guest_user @thing.save </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload