Anyway, it is certainly time to get back to my SLPapp, as I would like to finish it soon. One of the new features I wanted to implement is creating a guest user with Devise.
I first looked at the Devise wiki, whose approach is to create a guest user in a session. Unfortunately, the guest user would persist only as long as the browser session is open. For my purposes, I wanted the guest user to have almost the same functionality as a regular user. Specifically, I wanted the guest user to be able to create the same - or almost the same associations - as a regular user. In this case, it seemed appropriate to use an approach discussed in RailsCasts episode #393. The approach is as follows: create a guest user in a database, sign them in, and delete all guest users from the database using a rake task (for example, every 24 hours).
In ApplicationController: def create_guest_user
u = User.new { |user| user.guest = true }
u.email = "guest_#{Time.now.to_i}#{rand(100)}@example.com"
u.save!(:validate => false)
sign_in(:user, u)
redirect_to root_path
end
In routes.rb:match '/sign_in_guest', to: "application#create_guest_user", via: 'get'
In a view:<%= link_to "Guest Login", sign_in_guest_path %>
As a result, a user can click on a "Guest Login" link, and a unique guest user is generated and saved to the database automatically. The user can perform almost the same operations as a regular user. Currently, there is no option to save all the temp data if a guest user decides to sign up but I may reconsider this later.
No comments :
Post a Comment