I followed Railscast #393 to implement guest users into my application. The only problem I'm having with this approach is that it requires the user to still click a button to create the user (albeit without signing up). My goal is to have this occur automatically, but without happening every time the page is reloaded or visited again. I'm at a loss on how to go about this.
user_controller.rb
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = params[:user] ? User.new(params[:user]) : User.new_guest
if @user.save
current_user.move_to(@user) if current_user && current_user.guest?
session[:user_id] = @user.id
redirect_to root_url
else
render "new"
end
end
end
user.rb
class User < ActiveRecord::Base
attr_accessible :name, :provider, :uid, :email
has_many :posts, :dependent => :destroy
has_many :comments, :dependent => :destroy
def self.new_guest
new { |u| u.guest = true }
end
def move_to(user)
posts.update_all(user_id: user.id)
comments.update_all(user_id: user.id)
end
end
application.html.erb
<ul>
<li><%= button_to "Try it for free!", users_path, method: :post %></li>
</ul>
I can provide any additional information necessary to help answer this question.