簡體   English   中英

特定頁面未登錄rails dev

[英]rails devise not logged in for particular page

當我登錄時,它會重定向到根頁面,但是如果user_signed在這里,則在導航欄(標題)中? 函數未執行,但在其他頁面中,在標題中說/ browse / audi,它顯示具有注銷的用戶名,其余頁面標識用戶已登錄,但登錄頁面除外

 caches_page :index

layout 'application_slide', only: [:index]
layout 'car_model', only: [:car_model]
before_filter :authenticate_user!, only: "step2"

我的root_path控制器(即索引)

def index
if user_signed_in?
  if current_user.role == "dealer"
    redirect_to dashboard_dealer_path
  else
    #@carmake = Carmake.search(params[:makename])asd
    #    @carmodel = Car.search(params[:makename])
    #@zipcode = Profile.search(params[:search])
    @makes = DivisionDefinition.order(:division_name)
    ................
    ................
    ................
    ................
    end
  end
end

我的索引布局

<title><%= content_for?(:title) ? yield(:title) : "Nabthat" %></title>

<%= stylesheet_link_tag    "admin" %>

<%= csrf_meta_tags %>
</head>

<body class="login-layout off-canvas hide-extras" id="common-nav" cz-shortcut-listen="true">
 <%= render "/layouts/navbar" %>
 <%# render "/layouts/flash" %>
 <div class="row">
  <div class="main-container" id="main-container">
    <div class="main-container-inner">
      <%= yield %>
    </div>
  </div>
</div>

我的導航欄

    <% if !user_signed_in? %>
    <li>
      <%= link_to "Log in", "#nav-login-modal",:data => {"toggle" => "modal"}, class: "li-pull-right"%>  
    </li><li>
      <%= link_to "List Your Dealership", new_user_registration_path, class: "list-dealer-new nav-list-dealer"%>
    </li>
    <% else %>

僅對於索引操作,它會在導航欄中顯示登錄鏈接,就像我沒有登錄一樣,但是如果我轉到任何其他頁面,它將顯示用戶名

在使用curl檢查此錯誤時(黑白/根和其他頁面不同)

root_page

➜  nthat git:(master) ✗ curl -I https://nabthat.com/
HTTP/1.1 200 OK
Server: nginx/1.4.1 (Ubuntu)
Date: Fri, 07 Feb 2014 10:18:15 GMT
Content-Type: text/html
Content-Length: 21392
Last-Modified: Mon, 03 Feb 2014 09:04:20 GMT
Connection: keep-alive
ETag: "52ef5b94-5390"
Accept-Ranges: bytes

➜  nthat git:(master) ✗ curl -I https://nabthat.com/browse-car/Audi
HTTP/1.1 200 OK
Server: nginx/1.4.1 (Ubuntu)
Date: Fri, 07 Feb 2014 10:18:51 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 39651
Connection: keep-alive
Status: 200 OK
X-UA-Compatible: IE=Edge,chrome=1
ETag: "659ad8caeff9e921ced5ef8e3fa0d266"
Cache-Control: max-age=0, private, must-revalidate
Set-Cookie: request_method=GET; path=/
X-Request-Id: 30d83bc2f036eedffe1a8fdb1be7405b
X-Runtime: 0.363171
X-Rack-Cache: miss

首先檢查您的用戶模型,它應該是user.rb而不是users.rb

否則,您可以使用current_user方法來檢查是否已登錄的用戶

看起來caches_page :index在您登錄之前正在緩存頁面。然后,當您登錄時,將返回已緩存的頁面,而無需使用Rails應用服務器進行檢查(因此永遠不會有機會檢查您是否已登錄) -nginx只是直接返回它(這將解釋為什么curl收到的響應標頭如此不同)。

如果是這種情況,您可以通過刪除caches_page :index來解決。 如果您確實需要緩存索引頁,則可以將所需的大部分索引視圖包裝在<% cache do %> … <% end %>塊中(可能不多,因為看起來好像動態內容正在發生)索引視圖-例如角色檢查)。 另外,如果您使用的是Rails 4,則可能要使用動作緩存或新的Rails 4緩存方法。

Rails 3.2緩存: http//guides.rubyonrails.org/v3.2.16/caching_with_rails.html

Rails 4緩存: http//guides.rubyonrails.org/caching_with_rails.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM