I was wondering how you flip the flow of a website.
Example: Normally a visitor needs an user login to write a summary. When the visitor come to the page and click create summary, are the visitor normally redirect to sign up page.
How to flip the flow. So you can write a summary without login. And when you click next, you need to login or create a use开发者_如何转开发r. User decides to create an user and the summary are created with an association or the user have a login and it also creates and association.
How to create this with devise?
I suggest the following approach:
After creating the summary, store its ID in a session variable. Then, when the user signs in, if any summary IDs are found in their session, associate them with the signed in user
For example:
class SummaryController < ApplicationController
def create
@summary = Summary.create!(params[:summary])
unless user_signed_in?
session[:anon_summary_ids] ||= []
session[:anon_summary_ids] << @summary.id
redirect_to(user_sign_in_path, :notice => "Thanks for the summary! Please sign in...")
end
end
end
class ApplicationController
before_filter :associate_summaries, :if => [:user_signed_in?, :anon_summaries?]
private
def anon_summaries?
session[:anon_summary_ids].try(:any?)
end
def associate_summaries
Summary.where(:id => session[:anon_summary_ids], :user_id => nil).update_all(:user_id => current_user.id)
session[:anon_summary_ids] = nil
end
end
精彩评论