简体   繁体   English

特定页面未登录rails dev

[英]rails devise not logged in for particular page

when i sign in it redirects to the root page, but here in the navbar(header) if user_signed in ? 当我登录时,它会重定向到根页面,但是如果user_signed在这里,则在导航栏(标题)中? function is not executed, but in other pages lets say /browse/audi in the header it shows the username with logout, rest of the pages identifies that user has logged in except but the landing page 函数未执行,但在其他页面中,在标题中说/ browse / audi,它显示具有注销的用户名,其余页面标识用户已登录,但登录页面除外

 caches_page :index

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

My controller for the root_path (ie, index) 我的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

My layout for index 我的索引布局

<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>

My navbar 我的导航栏

    <% 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 %>

For index action alone it shows the login link in navbar as if i'm not logged in but if i go to any other page it shows user name 仅对于索引操作,它会在导航栏中显示登录链接,就像我没有登录一样,但是如果我转到任何其他页面,它将显示用户名

While checking with curl for this error (diffrence b/w root and other page) 在使用curl检查此错误时(黑白/根和其他页面不同)

root_page 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

First check your user model it should be user.rb not users.rb 首先检查您的用户模型,它应该是user.rb而不是users.rb

Otherwise you can use current_user method for checking the user logged in or not 否则,您可以使用current_user方法来检查是否已登录的用户

It looks like the caches_page :index is caching the page before you sign in. Then when you are signed in, the cached page is returned without checking with your rails app server (so never has a chance to check if you're signed in) - nginx just returns it directly (which would explain why the response headers received by curl are so different). 看起来caches_page :index在您登录之前正在缓存页面。然后,当您登录时,将返回已缓存的页面,而无需使用Rails应用服务器进行检查(因此永远不会有机会检查您是否已登录) -nginx只是直接返回它(这将解释为什么curl收到的响应标头如此不同)。

If this is the case, you could resolve it by removing the caches_page :index . 如果是这种情况,您可以通过删除caches_page :index来解决。 If you really need to cache your index page, you can wrap however much of the index view you want in a <% cache do %> … <% end %> block (possibly not much as it looks like there's dynamic stuff happening in your index view too - eg the role check). 如果您确实需要缓存索引页,则可以将所需的大部分索引视图包装在<% cache do %> … <% end %>块中(可能不多,因为看起来好像动态内容正在发生)索引视图-例如角色检查)。 Alternatively you might want to use action caching or the new Rails 4 caching approach if you're on Rails 4. 另外,如果您使用的是Rails 4,则可能要使用动作缓存或新的Rails 4缓存方法。

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

Rails 4 caching: http://guides.rubyonrails.org/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