2012-02-27 12 views
0

が動作していないハッシュルビ1.9.3p0(2011年10月30日改訂33570)[i686の-のLinux]レール3.2.1 redirect_toオプションは、レールを使用

マイHelloController.rb#sign_in方法 は、login_pageをレンダリングし、received.Ifの検証が失敗した資格証明書(フォーム値)を検証します。

検証が成功すると、資格情報が正しい見つかった場合、他のは

redirect_to "/games" # works 
に動作します、次の私の場合は 「/ハロー/ login_page」

にリダイレクトゲーム#インデックスにリダイレクトするユーザーの資格情報に を認証以下のいずれかが動作していない間

redirect_to {controller:"games", action:"index"} # Doesn't work 
URL:

 Output: 
     Routing Error 

     No route matches [GET] "/hello/sign_in"  

しかし私UsersController.rb番号からリダイレクトするためのオプションの同じスタイルを使用してhttp://localhost:3000/hello/sign_inは細かい作品をリダイレクトし、メソッドを作成

後者の場合は、私のようなエラーが表示されます。検証かかわらず続いている間、私は作品

redirect_to "/hello/login_page", :notice => "User Id and Password don't match" # works 

次のユーザーにユーザーID /パスワードがこのケースをmatch.Inないメッセージとログインページを表示する必要があります。また

資格情報が正しく見つからない場合

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(足場-generatedコントローラ)

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

名前付きルートの使用を検討しましたか? – Ekampp

答えて

0

あなたはthe rails guide's article about routingで命名ルートに関する詳細を読むことができます。

    あなたは、ゲームコントローラのためのshowアクションをターゲットに redirect_to game_path(@game)ような何かを行うことができ
  • 、および
  • redirect_to games_path(notice: "Some notice..")
  • は予告とindexアクションにリダイレクトします。
+0

RESTfulなルートのために、games_pathなどのヘルパーを使用しています。問題は発生しません。手動で作成されたコントローラの場合のみ、私はエラーに直面します。私の質問は、redirect_toメソッドでハッシュリテラルを使用する方です。redirect_to {コントローラ: "ゲーム"、アクション: "インデックス"} redirect_to "/ games"が動作している間は動作しません。なぜハッシュリテラルを渡すと機能が動作しないのですか? APIは、このメソッドがハッシュhttp://api.rubyonrails.org/classes/ActionController/Redirecting.html#method-i-redirect_toを受け入れることができることを明確に述べています。ありがとう。 –

関連する問題