レイアウトの部分的なRuby on Railsロジック
-
19-09-2019 - |
質問
ビューがコントローラーとどのように話し合うかと同様に、いくつかのロジックが含まれるサイドバーがあります。部分的なロジックをどこに入れることができますか?レイアウト用の新しいコントローラーを作成して、そこに入れますか?
レイアウトは、ダッシュボードのように、ユーザーにログインするためのものです。ダッシュボードには、すべてのページに同じ動的コンテンツを表示するサイドバーがあります。すべてのページに表示されているので、私はそれを部分的に入れたいと思います。
私は、発見をどこに置くか、そしてそのすべての情報を置くことについて混乱しています。
解決
Maybe there's a better way to do this, but if it's on most or all pages you could create a before_filter on your ApplicationController. You'll have to call skip_before_filter
on the controllers/actions that don't need the data.
class ApplicationController < ActionController::Base
before_filter :load_sidebar_data
protected
def load_sidebar_data
end
end
If you don't need it on most pages you'd still put the method in the ApplicationController, you'd just add the before filter where you need it.
他のヒント
Put the partial in the layouts folder. The name of the file should start with an underscore For example, "_mypartial.html.erb".
Then, in your views, use the following code to include the partial
"mypartial" %>Note that you do not include the underscore in the embedded ruby code when specifying which partial to render.
You might consider using Cells (view components for Rails): http://cells.rubyforge.org/