简体   繁体   English

如果用户未登录,则不重定向,但在用户未登录时设置重定向

[英]Not Redirecting if User not Logged in but Have a redirect set up for when user not logged In

I do not understand why it is loading the page instead of directly redirecting. 我不明白为什么它要加载页面而不是直接重定向。 It used to work I don't know what it could be. 它曾经可以工作,但我不知道它会是什么。

This is my application.erb layout template : 这是我的application.erb布局模板

<!DOCTYPE html>
<html>
<head>
  <title>Pandora</title>
  <!-- Include needed.css files for use -->
  <%= stylesheet_link_tag 'application' %>
  <%= stylesheet_link_tag 'utilities/bootstrap.min'%>
  <%= stylesheet_link_tag 'utilities/font-awesome.min' %>
  <%= stylesheet_link_tag 'elements/navbar' %>
  <%= stylesheet_link_tag 'helpers' %>
  <%= stylesheet_link_tag 'home' %>
  <!-- Include all existing js files for use -->
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
    <!-- Prelang: Analytics / Google Analytics -->
    <%= analytics_init if Rails.env.production? or Rails.env.development? %>
</head>
<body>

<% require_user_signed_in %>

<!-- Render the navbar -->
<%= render partial: "elements/navbar" %>
<!-- Render the sidebar -->
<%= render partial: "elements/sidebar" %>
  <!-- Moves main content to the side to leave place for sidebar -->
  <div class=""></div>
  <!-- MAIN CONTENT Start -->
      <!--main content start-->
      <section id="main-content">
        <section class="wrapper">
          <div class="row">
            <div class="col-lg-9 main-chart">
            <!-- Renderes the popup notifications -->
            <% if flash[:notice] %>
              <div class="notice"><%= flash[:notice] %></div>
            <% end %> <br>
              <!-- Renders everything in the page file -->
              <div class="main-move-up">
                <%= yield %>
              </div>
          </div><!-- /col-lg-9 END SECTION MIDDLE -->

          <div class="col-lg-3 ds">
            <%= render partial: "elements/notificationbar" %>
          </div><!--/col-lg-3 ds -->
        </section>
      </section>
      <!--main content end-->
      </section>

<!-- Renders the footer -->
<%= render partial: "elements/footer" %>


<!-- js placed at the end of the document so the pages load faster -->
<%= javascript_include_tag('dropdowns.js') %>

</body>
</html>

And I have this in my application controller : 我在我的应用程序控制器中有这个:

def require_user_signed_in
    unless user_signed_in?

      # If the user came from a page, we can send them back.  Otherwise, send
      # them to the root path.
      # if request.env['HTTP_REFERER']
      #   fallback_redirect = :back
      if defined?(root_path)
        fallback_redirect = root_path
      else
        fallback_redirect = "/"
      end

      redirect_to fallback_redirect, flash: {error: "You must be signed in to view this page."}
    end
  end

My problem: I am getting the error undefined method 'id' for nil:NilClass from <%@companies=Company.where(:user_id => current_user.id)%> (located in home.erb); 我的问题:我从<%@companies=Company.where(:user_id => current_user.id)%> (位于home.erb)中undefined method 'id' for nil:NilClass的错误undefined method 'id' for nil:NilClass this is normal since current_user doesn't exists when loading the page and not logged in. However what is not normal is that it is loading <%= yield %> which loads home.erb . 这是正常的,因为在加载页面且未登录时current_user不存在。但是,不正常的是它正在加载<%= yield %> ,它加载了home.erb It should load the method <% require_user_signed_in %> (located at the top of the template) first shoudn't it? 它应该首先加载方法<% require_user_signed_in %> (位于模板顶部)吗? And therfore redirect before it loads the rest of the page. 然后重定向,然后再加载页面的其余部分。

Please let me know if you would like me to post more of my files from the project. 如果您希望我发布该项目中的更多文件,请告诉我。

You might be interested in Controller Filters . 您可能对Controller Filters感兴趣。 From the RoR Guides: 从RoR指南中:

class ApplicationController < ActionController::Base
    before_action :require_login
     
    private
     
    def require_login
        unless logged_in?
            flash[:error] = "You must be logged in to access this section"
            redirect_to new_login_url 
        end
    end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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