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

时间:2015-08-07 11:23:37

标签: ruby-on-rails redirect devise

我不明白为什么加载页面而不是直接重定向。过去工作我不知道它可能是什么。

这是我的 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>

我在应用程序控制器中有这个:

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

我的问题:我从undefined method 'id' for nil:NilClass(位于home.erb)收到错误<%@companies=Company.where(:user_id => current_user.id)%>;这是正常的,因为当加载页面并且未登录时current_user不存在。但是,正常情况是加载<%= yield %>加载home.erb。它应该首先加载方法<% require_user_signed_in %>(位于模板的顶部)吗?并且在加载页面的其余部分之前进行重定向。

如果您希望我从项目中发布更多文件,请告诉我。

1 个答案:

答案 0 :(得分:1)

您可能对Controller Filters感兴趣。来自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