销毁会话问题Rails 4

时间:2013-11-15 19:14:24

标签: ruby-on-rails ruby routes session-variables

我遇到了破坏会话的问题,链接似乎不起作用,地址栏中的URL从/ dashboard更改为/ log_out,但页面未重定向到登录页面。我对此感到困惑。

仪表板视图:

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

1 个答案:

答案 0 :(得分:1)

请勿使用defined?检查是否存在哈希值。它将始终返回非假值:

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

有关详细信息,请参阅docs on this

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