2013-11-15 67 views
0

我在销毁会话时出现问题,链接似乎不起作用,地址栏中的URL从/ dashboard更改为/ log_out,但该页面未重定向到登录页面。我对此感到莫名其妙。销毁会话问题Rails 4

仪表板视图:

<% if logged_in? %> 
    <% if request.env['mobvious.device_type'] == :mobile %> 
     <div class="container" style="width: 90%" > 
      <table> 
       <tr> 
        <td class="dash_cont"> 
         <%= link_to "Logout", log_out_path %> 
        </td> 
       </tr> 
       <tr> 
        <td class="dash_cont"> 
         <h1 class="form-signin-heading" >Welcome to your personal Twitter Manager!</h1> 
         <p>Use this site to keep track of your followers, see how many posts they've made and many more features!</p> 
         <p>It's a really cool site, check it out! It's free!</p><br/><br/> 
        </td> 
       </tr> 
      </table> 
     </div> 
    <% elsif request.env['mobvious.device_type'] == :desktop %> 
     <div class="container" style="width: 70%;" > 
      <table> 
       <tr> 
        <td class="dash_cont"> 
         <%= link_to "Logout", log_out_path %> 
        </td> 
       </tr> 
       <tr> 
        <td class="dash_cont"> 
         <h1 class="form-signin-heading" >Welcome to your personal Twitter Manager!</h1> 
         <p>Use this site to keep track of your followers, see how many posts they've made and many more features!</p> 
         <p>It's a really cool site, check it out! It's free!</p><br/><br/> 
        </td> 
       </tr> 
      </table> 
     </div> 
    <% end %> 
<% else %> 
    <script type="text/javascript"> 
     window.location.href="/" // put your correct path in a string here 
    </script> 
<% end %> 

登录查看:

<% if logged_in? %> 
    <script type="text/javascript"> 
     window.location.href="/dashboard" // put your correct path in a string here 
    </script> 
<% else %> 
    <% if request.env['mobvious.device_type'] == :mobile %> 
     <div class="container" style="width: 90%" > 
      <table> 
       <tr class="login-cont"> 
        <td class="login-tcell"> 
         <h1 class="form-signin-heading" >Log in</h1> 
         <%= form_tag sessions_path, class: "form-signin" do %> 
          <%= text_field_tag :email, params[:email], class: "form-control", autofocus: "", required: "", placeholder: "Email address" %> 
          <%= password_field_tag :password, nil, class: "form-control", required: "", placeholder: "Password" %></br> 
          <p class="button"><%= submit_tag "Log in", class: "btn btn-lg btn-primary btn-block" %></p> 
         <% end %> 
         <%= link_to "Register", "/sign_up" %> 
         <% if defined?(@error) %> 
          <div class="error"> 
           <%= "*" + @error %> 
          </div> 
         <% end %> 
         <% if defined?(@notice) %> 
          <div class="error"> 
           <%= "*" + @notice %> 
          </div> 
         <% end %> 
        </td> 
       </tr> 
      </table> 
     </div> 
    <% elsif request.env['mobvious.device_type'] == :desktop %> 
     <div class="container" style="width: 70%" > 
      <table> 
       <tr class="login-cont"> 
        <td class="login-tcell"> 
         <h1 class="form-signin-heading" >Log in</h1> 
         <%= form_tag sessions_path, class: "form-signin" do %> 
          <%= text_field_tag :email, params[:email], class: "form-control", autofocus: "", required: "", placeholder: "Email address" %> 
          <%= password_field_tag :password, nil, class: "form-control", required: "", placeholder: "Password" %></br> 
          <p class="button"><%= submit_tag "Log in", class: "btn btn-lg btn-primary btn-block" %></p> 
         <% end %> 
         <%= link_to "Register", "/sign_up" %> 
         <% if defined?(@error) %> 
          <div class="error"> 
           <%= "*" + @error %> 
          </div> 
         <% end %> 
         <% if defined?(@notice) %> 
          <div class="error"> 
           <%= "*" + @notice %> 
          </div> 
         <% end %> 
        </td> 
        <td class="welcome-msg white"> 
         <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/> 
         <h1 class="form-signin-heading" >Welcome to your personal Twitter Manager!</h1> 
         <p>Use this site to keep track of your followers, see how many posts they've made and many more features!</p> 
         <p>It's a really cool site, check it out! It's free!</p><br/><br/> 
        </td> 
       </tr> 
      </table> 
     </div> 
    <% end %> 
<% end %> 

仪表板控制器:

class DashboardController < ApplicationController 

    helper_method :logged_in? 

    def new 
    end 
    def logged_in? 
     if defined? session[:user_id] 
      true 
     else 
      false 
     end 
    end 
end 

会话控制器:

class SessionsController < ApplicationController 

    helper_method :logged_in? 

    # Use this to detect device type: 
    # request.env['mobvious.device_type'] 

    def new 
    end 
    def create 
     user = User.authenticate(params[:email], params[:password]) 
     if user 
      session[:user_id] = user.id 
      @notice = "Logged in!" 
      redirect_to "/dashboard" 
     else 
      @error = "Invalid email or password" 
      render "new" 
     end 
    end 

    def destroy 
     session[:user_id] = nil 
     redirect_to root_url, :notice => "Logged out!" 
    end 
    def logged_in? 
     if defined? session[:user_id] 
      redirect_to "/dashboard" 
     else 
      redirect_to "/" 
     end 
    end 
end 

路线:

TwitterApp::Application.routes.draw do 
    get "dashboard/new" 
    get "users/new" 
    get "sessions/new" 
    get "log_out" => "sessions#destroy", :as => "log_out" 
    get "" => "sessions#new", :as => "log_in" 
    get "sign_up" => "users#new", :as => "sign_up" 
    get "dashboard" => "dashboard#new", :as => "dashboard" 
    root :to => "sessions#new" 
    resources :users 
    resources :sessions 
end 
+0

Conner,Ruby on Rails问题不应该用Ruby标记。查看Ruby标签wiki。请删除红宝石标签。 – screenmutt

回答

1

不要使用defined?检查散列值的存在。它总是会返回一个非假值:

some_hash = { :some_key => nil } 
defined? some_hash[:some_key] # => "method" 
defined? some_hash[:i_dont_exist] # => "method" 

更多信息,请参见docs on this

在导轨上,您可以使用session[:user_id].present?。这将检查该键处的值不是空白或零。另外从会话中删除的更好方法是在其上调用deletesession.delete(:user_id)session.clear如果您想擦除所有内容。

+0

谢谢老兄,定义?方法是问题,我没有意识到它只是检查变量是否被初始化,我认为它是检查它是否有价值。白痴,谢谢你:) –