RailsTutorial Sessions: вход в систему не мигает успешно, а выход вызывает ошибку маршрутизации - PullRequest
0 голосов
/ 07 марта 2012

После обучения Хартла.Когда я пытаюсь войти, я перехожу на правильную страницу, но "Успешный вход!"не мигает на интерфейсе.Кроме того, когда я пытаюсь выйти из системы, я получаю сообщение об ошибке маршрутизации: No route matches [GET] "/sessions/1".

Любая помощь будет принята!

Вот мой сеанс_controller.rb:

class SessionsController

def new
  @title = "Log in"
end

def create
  user = User.authenticate(params[:session][:email], 
                           params[:session [:password])     
  if user.nil?
    flash.now[:error] = "Invalid email/password combination."
    @title = "Sign in"
    render 'new'
  else
    flash[:success] = "Successful login!"
    sign_in user
    redirect_back_or user
  end
end

def destroy
  sign_out
  redirect_to root_path
end

end

Вот мои маршруты.rb:

SampleApp::Application.routes.draw do

  #resources :users    this is the line I removed to get my code to work
  resource :user #this is the lined I added to get my code to work

  resources :sessions, :only => [:new, :create, :destroy]   


  match '/sessions', :to => 'users#show'
  match '/signup', :to => 'users#new'
  match '/login', :to => 'sessions#new'
  match '/logout', :to => 'sessions#destroy'

  root :to => 'pages#home'

Изменение resources :users на resource :user, кажется, позволяет мне "входить" и "выходить из системы"msgstr "за исключением того, что флеш-память все еще не отображается в пользовательском интерфейсе.Это заставляет меня поверить, что в моем методе создания есть ошибка.

Вот мой session_helper.rb:

module SessionsHelper

def sign_in(user)
  cookies.permanent.signed[:remember_token] = [user.id, user.salt]
  self.current_user = user
end


def signed_in?
  !current_user.nil?
end

def current_user=(user)
  @current_user = user
end

def current_user
  @current_user ||= user_from_remember_token
end

def current_user?(user)
  user == current_user
end

def sign_out
  cookies.delete(:remember_token)
  self.current_user = nil
end

def redirect_back_or(default)
  redirect_to(session[:return_to] || default)
  clear_return_to
end

private

def user_from_remember_token
  User.authenticate_with_salt(*remember_token)
end

def remember_token
  cookies.signed[:remember_token] || [nil, nil]
end

def store_location
  session[:return_to] = request.fullpath
end

def clear_return_to
  session[:return_to] = nil
end
end

Отредактировано: Решение опубликовано в коде.

1 Ответ

3 голосов
/ 07 марта 2012

я думаю, что вы должны использовать только flash[:success] и flash[:error], а не flash.now

, и какую ссылку вы используете для выхода?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...