使用Rails: 3.2.1 ,Ruby: ruby 1.9.3p0(2011-10-30修订版33570)[i686-linux] 。
我的 HelloController.rb#sign_in 方法 验证收到的凭据(表单值)。如果验证失败,则呈现 login_page 。
验证成功后,验证用户凭据 如果找到的凭据正确,请重定向到游戏#index ,否则重定向到“/ hello / login_page”
在我的案例中关注作品
redirect_to "/games" # works
而下面的那个不起作用
redirect_to {controller:"games", action:"index"} # Doesn't work
后一种情况向我显示如下错误: 网址:http://localhost:3000/hello/sign_in
Output:
Routing Error
No route matches [GET] "/hello/sign_in"
但是,使用相同样式的选项从我的 UsersController.rb #create 方法重定向,重定向工作正常。
此外,如果找不到正确的凭据,但是经过验证,我需要向用户显示登录页面,其中包含用户名/密码不匹配的消息。在这种情况下,后续工作
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
我想了解这种行为背后的原因是什么。
以下是我的代码:
的routes.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
谢谢,
Jignesh
答案 0 :(得分:0)
您可以在the rails guide's article about routing中阅读有关命名路线的更多信息。
redirect_to game_path(@game)
之类的操作来定位游戏控制器的show动作,redirect_to games_path(notice: "Some notice..")
通过通知重定向到索引操作。