ruby on rails - How to let a non-signed in user access a restricted page only once? -


i have rails 3.2 app requires user signed in (current_user) in order access event pages, subdomains. i'm using devise authentication.

is there way allow user one-time access event page if supplied direct link it? want them prompted sign in (or sign up) if try access different event pages, or if leave , come @ future date same event page.

i've watched guest account episode on railscasts, seems user continue logging in guest without ever signing approach.

here events controller code:

def show     @event = event.find_by_name(request.subdomain)     if params[:id].present?       @event = event.find(params[:id])     end     # if @event.present? , @event.videos.present?       @video = video.find_by_id(params[:video]) || @event.videos.first     # else      #   @video = @event.videos.first     # end     # @json = event.all.to_gmaps4rails      if @user = current_user     else        flash[:alert] = "please sign in first"       redirect_to sign_up_url(:subdomain => false)     end end 

thanks help/advice...

edit: provide more context:

  1. i trying drive lot of users 1 event page each week, , i'm not sure it's possible/practical hardcode separate hashes urls if i'm sending out 1 link via social media , email etc.

  2. as users click on link , go straight page in question, there no action being triggered create separate guest model, think answer have session based, adding column existing user model, ensuring used once per user. i'm thinking maybe ip address work?

the prescient solution create session variable guest users containing boolean value indicating whether or not have visited page or not (and consequently enabling access if have not visited, , vice-versa):

# in controller before_filter :check_guest, :only => :show  private  def check_guest   # if user isn't logged in   if current_user.nil?      # if user has viewed, redirect     if session[:viewed] == true       flash[:alert] = "please sign in first"       redirect_to sign_up_url(:subdomain => false)     # if user hasn't viewed, allow access, flag having viewed     else       session[:viewed] = true     end   end end 

though using sessions de-facto approach particular issue, there limits implementation:

  • session must remain valid in order of guest access tracked
  • browser-specific (thus multiple users on browser share same session)

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -