3

条件が真の場合にユーザーをリダイレクトしたい:

class ApplicationController < ActionController::Base
  @offline = 'true'
  redirect_to :root if @offline = 'true'
  protect_from_forgery
end

編集 これは私が今試みていることですが成功していません。デフォルトのコントローラーはアプリケーションコントローラーではありません。

class ApplicationController < ActionController::Base
    before_filter :require_online 

    def countdown

    end

private
  def require_online
    redirect_to :countdown
  end

end

これにより、ブラウザエラーが発生しますToo many redirects occurred trying to open “http://localhost:3000/countdown”. This might occur if you open a page that is redirected to open another page which then is redirected to open the original page.

追加する&& returnと、アクションが呼び出されることはありません。

4

3 に答える 3

4

Jedの答えに加えて

代入演算子ではなく、比較演算子が必要です。

 redirect_to :root if @offline == 'true'

さらに問題がある場合は、次の方法でテストを簡素化します。

 redirect_to(:root) if @offline == 'true'

それとも、文字列ではなく真のブール値である必要がありますか?

 redirect_to :root if @offline

class ApplicationController < ActionController::Base
   before_filter :require_online 

 private
    def require_online
       redirect_to(:root) && return if @offline == 'true'
    end
 end
于 2011-07-01T21:14:39.877 に答える
1

Redirect_toはアクションから呼び出す必要があります。

例として、

class ApplicationController < ActionController::Base
  protect_from_forgery

  def index
    @offline = 'true'

    // Once you do the redirect make sure you return to avoid a double render error. 
    redirect_to :root && return if @offline == 'true' // Jed was right use a comparison 
  end
end

「リダイレクト」ドキュメントをご覧ください

于 2011-07-01T19:32:55.337 に答える
0

代入演算子ではなく、比較演算子が必要です。

redirect_to :root if @offline == 'true'

さらに問題が発生した場合は、次の方法でテストを簡略化してください。

redirect_to(:root) if @offline == 'true'

それとも、文字列ではなく真のブール値である必要がありますか?

redirect_to :root if @offline
于 2011-07-01T19:19:08.510 に答える