3

localhost:3000/dashboardは、ユーザーのタイプに基づいて異なるビュー/コントローラーの組み合わせを指し示す必要がある場合があります。私のアプリケーションの主な2つのタイプはSubscriberPublisherです。同じ名前空間だが、Railsのコントローラーとビューが異なる

Publisherログインして/dashboardに行くと、私はパブリッシャーダッシュボードを表示する必要があります。

Subscriberがログインし、/dashboardに行くと、私はサブスクライバダッシュボードを表示する必要があります。

この時点で、パブリッシャーのダッシュボードはDashboardと呼ばれ、サブスクライバーのダッシュボードはProfileと呼ばれています。少し汚れているようです。

質問があります。適切なコントローラを呼び出し、適切なデータをロードし、特定のユーザのタイプに基づいて適切なテンプレート/レイアウトをレンダリングする最良の方法は何ですか?

答えて

2

私はあなたのために次のような疑似コードを検討します。

コントローラー:

app/controllers/dashboard_controller.rb 

class Dashboard < ApplicationController 

def index  
    render, :user_type => current_user.user_type 
end 

ビュー: (表示されるかを変更するにはヘルパーを使用してください)。

views/dashboards/index.html.erb 

# display the content 

ヘルパー。

helpers/dashboard_helper.rb 
module DashboardHelper(user_type) 
if user_type == 'publisher' 
    #set content/variables for publisher 
elsif user_type == 'Subscriber' 
    #set content/variables for subscriber 
else 
    set content/variables to default. 
end 
+0

ありがとう!しかし、私がそれを実装する方法はあなたの提案とは異なり、私はあなたの提案に従った。コードは次のようになります: 'def index' ' @publisher = @subscriber = current_user' 'レンダリングテンプレート:"#{current_user.type.underscore.pluralize} /ダッシュボード "' 'end' – Ivan

関連する問題