使用 Rails:3.2.1,Ruby:ruby 1.9.3p0(2011-10-30 修订版 33570)[i686-linux]。
我的HelloController.rb#sign_in方法验证收到的凭据(表单值)。如果验证失败,它会呈现login_page。
验证成功后,验证用户凭据如果发现凭据正确,则重定向到Games#index否则重定向到“/hello/login_page”
在我的情况下,以下作品
redirect_to "/games" # works
而下面的一个不起作用
redirect_to {controller:"games", action:"index"} # Doesn't work
后一种情况向我显示如下错误: URL: http://localhost:3000/hello/sign_in
Output:
Routing Error
No route matches [GET] "/hello/sign_in"
但是,从我的UsersController.rb#create方法使用相同样式的重定向选项,重定向工作正常。
此外,如果未找到正确的凭据,尽管经过验证,我需要向用户显示登录页面,其中包含用户 ID/密码不匹配的消息。在这种情况下,以下工作
redirect_to "/hello/login_page", :notice => "User Id and Password don't match" # works
虽然跟随不起作用
redirect_to {controller:"hello", action:"login_page" notice: "User Id and Password don't match" } # Doesn't work
我想了解这种行为背后的原因是什么。
以下是我的代码:
路线.rb
MyGames::Application.routes.draw do
resources :users do
# Collection route
collection do
get 'register' # creates the register_users_url and register_users_path route helpers.
end
end
resources :games
get "hello/index"
get "hello/login_page"
post "hello/sign_in"
end
HelloController.rb(手动创建的控制器)
class HelloController < ApplicationController
skip_before_filter :require_login
skip_before_filter :require_admin_role
def index
# Show index page
end
def login_page
self.page_title = "Log In"
end
def sign_in
# Validates the credentials.If validation fails
# render "login_page"
# Once validation is successful, authenticates the user credentials
# If credentials found correct, redirect to Games#index
# else redirect to "/hello/login_page"
# In my case following works
# redirect_to "/games", while
# the one below does not work
# redirect_to {controller:"games", action:"index"} # Doesn't work
end
private
def page_title=(page_title)
@page_title = page_title
end
end
GamesController.rb(脚手架生成的控制器)
class GamesController < ApplicationController
# This is the only piece I have added
skip_before_filter :require_admin_role, :only => [:index]
# GET /games
# GET /games.json
def index
end
# GET /games/1
# GET /games/1.json
def show
end
# GET /games/new
# GET /games/new.json
def new
end
# GET /games/1/edit
def edit
end
# POST /games
# POST /games.json
def create
end
# PUT /games/1
# PUT /games/1.json
def update
end
# DELETE /games/1
# DELETE /games/1.json
def destroy
end
end
UsersController.rb(脚手架生成的控制器)
class UsersController < ApplicationController
skip_before_filter :require_login, :require_admin_role, :only => [:register, :create]
# GET /users
# GET /users.json
def index
end
# GET /users/1
# GET /users/1.json
def show
end
# GET /users/new
# GET /users/new.json
def new
end
# GET /users/register
def register
@user = User.new
end
# GET /users/1/edit
def edit
end
# POST /users
# POST /users.json
def create # Customized by me
@user = User.new(params[:user])
@user.role = 0 if @user.role.nil?
action_identifier = params[:action_identifier]
is_valid = @user.valid?
validation_result_options = get_validation_result_options(is_valid, action_identifier)
if is_valid
@user.save(:validate => false)
validation_result_options[:id] = @user.id if action_identifier != registration_action_identifier
# Save the user ID in the session so it can be used in
# subsequent requests
session[:current_user_id] = @user.id
# In this case it works correctly, by redirecting to '/games'
# when 'registration' action is found.
# The returned options are { controller: "games", action: "index" }
# which becomes
# redirect_to { controller: "games", action: "index" }
redirect_to validation_result_options
else
render validation_result_options
end
end
# PUT /users/1
# PUT /users/1.json
def update
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
end
private
def get_validation_result_options(is_valid, action_identifier)
if is_valid
options = case action_identifier
when registration_action_identifier
{ controller: "games", action: "index" }
else
{ action: "show", notice: 'User was successfully created.' }
end
else
options = case action_identifier
when registration_action_identifier
{ action: "register" }
else
{ action: "new" }
end
end
options
end
def registration_action_identifier
"registration"
end
end
ApplicationController.rb(脚手架生成的控制器)
class ApplicationController < ActionController::Base
protect_from_forgery
# Reference: http://guides.rubyonrails.org/action_controller_overview.html#filters
before_filter :require_login
before_filter :require_admin_role
# private methods are accessible by sub-classes
private
def current_user
@_current_user ||= session[:current_user_id] && User.find_by_id(session[:current_user_id])
end
def require_login
unless logged_in?
redirect_to controller: "hello", action: "login_page", notice: "You must be logged in to access this section"
end
end
def require_admin_role
unless is_admin?
redirect_to controller: "hello", action: "login_page", notice: "You must have admin role to execute this action"
end
end
def logged_in?
!current_user.nil?
end
def is_admin?
logged_in? && current_user.role == 1
end
end
谢谢,
吉涅什