0

我需要用户登录并被定向到 localhost:3000/users。在完成在线教程后,我有点迷失了,所以我会很感激任何帮助。

路线.rb

  resources :users

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

  root  'static_pages#home'

  match '/signup',  to: 'users#new',            via: 'get'
  match '/signin',  to: 'sessions#new',         via: 'get'
  match '/signout', to: 'sessions#destroy',     via: 'delete'

  match '/help',    to: 'static_pages#help',    via: 'get'
  match '/about',   to: 'static_pages#about',   via: 'get'
  match '/contact', to: 'static_pages#contact', via: 'get'

session_controller.rb

class SessionsController < ApplicationController

  def new
  end

  def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      sign_in user
      redirect_back_or user
    else
      flash.now[:error] = 'Invalid email/password combination'
      render 'new'
    end
  end

  def destroy
    sign_out
    redirect_to root_url
  end
end
4

2 回答 2

1

尝试这个:

def create
  user = User.find_by(email: params[:session][:email].downcase)
  if user && user.authenticate(params[:session][:password])
    sign_in user
    redirect_back_or users_url # <--- Changes
  else
    flash.now[:error] = 'Invalid email/password combination'
    render 'new'
  end
end
于 2013-09-13T14:42:17.783 回答
-1

您可以尝试以下操作,它会在登录后重定向到用户索引页面。

match 'users' => 'users#index', via: :get, as: :user_root

然后试试这个

session_controller.rb

def create
   user = User.find_by(email: params[:session][:email].downcase)
   if user && user.authenticate(params[:session][:password])
     sign_in user
     redirect_to user_root_path
   else
     flash.now[:error] = 'Invalid email/password combination'
     render 'new'
   end
end
于 2013-09-13T13:58:11.130 回答