0

レール 3.2.3

クラス変数をビューに渡す必要があります。何らかの理由で、これを行うことができません。

   class HomeController < ApplicationController
      @@user_id = '1343454'
      def index
       #.......................
      end
    def about
       #.......................
      end
    end

    /view/home/about.html.erb
    <% .... 
      @@user_id is not visible 
    ... %>

それを行う最も簡単な方法は何ですか?

4

1 に答える 1

1

使用しないでください@@

あなたapplication controllerは定義することができます:

def current_user
  @current_user.id = '1343454'
end
helper_method :current_user

ヘルパー メソッドはcurrent_user、アプリケーションの任意のコントローラーまたはビューで を使用できるようにします。

「1343454」は単なる例だと思います。通常、次のようなものがあります。

@current_user ||= User.find(session[:user_id]) if session[:user_id]
于 2012-07-24T20:06:17.873 に答える