简体   繁体   中英

logged_in_user not working bypassing landing page for logged in users

I've seen a few similar posts about this but they didn't seem to resolve my problem.

Problem It simply doesn't work. Doesn't error out, nothing, just doesn't evaluate and I don't know why because it does for other things?

Here is my Welcome controller definition:

  def show
    if logged_in?
      # raise  #I raise an error here and nothing happens on the web page. Just to confirm this isn't working for my sanity. 
      redirect_to recipes_url
    else
      render template: "welcome/#{params[:welcome]}"
    end
  end

The function logged_in? is defined in my /helpers/sessions_helper.rb:

  def logged_in?
    !current_user.nil?
  end

Here is the current_user:

  def current_user
    if (user_id = session[:user_id])
      @current_user ||= User.find_by(id: user_id)
    elsif (user_id = cookies.signed[:user_id])
    #  raise #The test still pass, so this branch is currently untested.
      user = User.find_by(id: user_id)
      if user && user.authenticated?(cookies[:remember_token])
        log_in user
        @current_user = user
      end
    end

And my routes are:

 get 'welcome/:welcome' => 'welcome#show'
 root :to => 'welcome#home'

Why doesn't this work? It just stays on the welcome page regardless of if I'm logged in or not. I know that logged_in? is evaluating correctly because I have other links on the page that are dynamic based upon this.

EDIT: What's really weird is that the logged_in? works in my view just not in this controller.

Here's a snippet from the nav of in the application layout view.

  <% if logged_in? %>
      <li><%= link_to "Profile", current_user %></li>
      <li><%= link_to "Log out", logout_path, method: "delete" %></li>
    <% else %>
      <li><%= link_to "Browse recipes", recipes_path %></li>
      <li><%= link_to "Log in", login_path %></li>
    <% end %>

Picture of the resulting site

logged_in_user isn't actually doing or returning anything if the user is already signed in. It just returns nil which evaluates to false, so there's no way for your show action to even reach the redirect_to recipes_url statement.

Did you intend to use the logged_in? function? This should work, provided logged_in? does what its name implies:

   def show
    if logged_in?
      redirect_to recipes_url
    else
      render template: "welcome/#{params[:welcome]}"
    end
  end

If using devise, make sure you're using a model named User , have devise defined in your routes.rb with devise_for :users and have devise declared on your model with at least the following:

 devise :database_authenticatable,
         :registerable,
         :rememberable,
         :validatable

Ok. The logged_in method was working fine. The reason that this wasn't working was because my route was going directly to the view 'welcome#home' and bypassing the controller altogether.

As soon as I made the route 'welcome#show' I was able to get this to work.

Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM