session - Keeping information linked to a non signed in user un rails -
i have rails application devise. when user signed in (user_signed_in?), can access , retrieve information (eg current_user.last_search). way save information linked session, such last search user made.
now, have code this:
<% if user_signed_in %> <%= current_user.last_search %> <% else %> <%= get_last_seach # method last search when user not signed in %> it's verbose. , need in lot of places, , other variables last_search.
i instead:
<%= my_current_user.last_search %> where my_current_user devise current_user when user logged in, else it's user linked current session (via cookies) information available.
my_current_user behave that:
# user starts new session on website user_signed_in? # false my_current_user # object user my_current_user.last_search # '' #user searches "pancakes" user_signed_in? # false my_current_user # object user my_current_user.last_search # 'pankakes' #user logs in user_signed_in? # true my_current_user # object user my_current_user.last_search # 'pankakes' #user logs off user_signed_in? # false my_current_user # object user my_current_user.last_search # 'pankakes' or '', don't mind here is aproach ? suggest ? if idea, how implement ?
you can create helper method in application helper this:
def last_search user_signed_in? ? current_user.last_search : session[:last_search] end and call controllers.
unless need remember user's last search after logging out , logging in again, may keep search in session users (regardless logged in or not).
Comments
Post a Comment