How to implement such a gradual registration / lazy registration in Rails?

Once upon a time I came across a website (I, unfortunately, lost my address, it was some kind of newspaper website), which allowed you to use everything as if you were a registered user. You can rate, like and comment on articles, and when you do this, a hidden built-in message will be displayed saying that you need to register on the website so that your contributions are saved. Then you had a link for you to see how your profile would look if you did it, and I was surprised to see that I had everything in my activity; articles that I read and saved, comments, etc. I left the site, and when I returned to it later, just out of curiosity, it still retained my activity.

I thought this was the greatest event, and now that I am creating a website with social features, I would also like to take this approach. But I still really love noob, and so I don’t know how to do this. How do you do this?

+5
source share
2 answers

Profile, , , .. Profile , . cookie . , cookie , ( , !).

, Profile User cookie . User.

Profile , .

, AnonymousProfile Profile ( ) - .

Update: . before_filter, , , , :

class ApplicationController < ActionController::Base
  before_filter :fetch_user_data

  def fetch_user_data
    @current_user = ... # Work your magic to get current user
  end

  private

  def current_profile
    @current_user and @current_user.profile  # Use profile association
  end
end

- :

if current_profile
  # Do stuff with current_profile
  # Only available to registered users...
end

current_profile, , .

+8

- cookie. , , ,:

"users" "". cookie , .

, (, , / ..) .

, .

...

, , before_filter _controller.rb. :

if has_cookie
   @user = lookup_user
else
   @user = create_new_guest_user
end

, act_as_authenticated , .

+2

All Articles