2017-03-03 5 views
0

RubyのRubyアプリケーションでは、各ページにナビゲーションビューをレンダリングする標準ビューがありますが、すべてのページで使用したい通知コントローラがあります。すべてのページにこのコントローラとビューをロードすると、これが可能1ページに無制限のコントローラ

この<%= render 'shared/navigation' %>は、ビューをレンダリングしているので、使用はない 私は別のコントローラ内の任意のヘルプ

psのための基本的

おかげで、コントローラを実行する実行しようとすると、イム。私は周りをよく見を持っていたし、

通知コントローラコード

class NotificationsController < ApplicationController 
    before_action :set_notification, only: [:show, :edit, :update, :destroy] 

    # GET /notifications 
    # GET /notifications.json 
    def index 
    @notificationlastid = Notification.last 
    # if the id params is present 
    if params[:id] 
     # get all records with id less than 'our last id' 
     # and limit the results to 5 
     @notifications = Notification.where('id > ?', params[:id]).where(userid: current_user.id) 
    else 
     @notifications = Notification.where(userid: current_user.id) 
    end 
    respond_to do |format| 
     format.html 
     format.js 
    end 
    end 

    # GET /notifications/1 
    # GET /notifications/1.json 
    def show 
    end 

    # GET /notifications/new 
    def new 
    @notification = Notification.new 
    end 

    # GET /notifications/1/edit 
    def edit 
    end 

    # POST /notifications 
    # POST /notifications.json 
    def create 
    @notification = Notification.new(notification_params) 

    respond_to do |format| 
     @notification.userid = current_user.id 
     if @notification.save 
     format.html { redirect_to @notification, notice: 'Notification was successfully created.' } 
     format.json { render action: 'show', status: :created, location: @notification } 
     else 
     format.html { render action: 'new' } 
     format.json { render json: @notification.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /notifications/1 
    # PATCH/PUT /notifications/1.json 
    def update 
    respond_to do |format| 
     if @notification.update(notification_params) 
     format.html { redirect_to @notification, notice: 'Notification was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: 'edit' } 
     format.json { render json: @notification.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /notifications/1 
    # DELETE /notifications/1.json 
    def destroy 
    @notification.destroy 
    respond_to do |format| 
     format.html { redirect_to notifications_url } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_notification 
     @notification = Notification.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def notification_params 
     params.require(:notification).permit(:content, :description) 
    end 
end 
+0

同じページの「複数のコントローラ」からのコンテンツをレンダリングするための唯一の方法は、AJAX.Theメインコントローラは、ページのサーバー側のレンダリングの他のチャンクを使用することですページはAJAXによって描画されます(他のコントローラに当たる)。私が本当にあなたがやろうとしていることを本当に理解しているかわかりません。複数のコントローラを使用すると、実際にはどういう意味ですか? – Codextremist

+0

よく通知ページ用のコントローラを作ったが、通知用のすべてのページにドロップダウンボックスを表示する – Ray

+0

通知コントローラメソッドをアプリケーションコントローラに移動し、フィルタを使用して各アクションの前に呼び出す必要があると思う。ビューコードは、アプリケーションレイアウトファイルの一部を使用してレンダリングできます。 – webster

答えて

2

に時間当たり1つのコントローラによってレンダリングされたすべてのサーバーサイドのページをやろうと何イムスイート何かを見つけることができません。これがレールの仕組みです。 その後、ページがロードされたら、クライアント側のコード(javascript)を使用して、 "NotificationsController"から通知を取得できます。これは実際にはかなり一般的なパターンであり、あなたの問題を解決する良い方法です。 ユーザーがページを確認している間に新しい通知が発生したとします。彼らは

例に到着しながら、簡単なAjaxコード、ポーリング「NotificationsController」すべてのX秒で、あなたも通知がユーザーにプリントアウトしていることができます。shared/_notifications.html.erb

<script> 
$(function() { 
    $.ajax({ 
    url: <%= [path_to_your_notifications_controller] %> 
    }).done(function(data) { 
    $("#mynotificationsdiv").html(data); 
    }); 
}); 
</script> 

は、今あなたがにHomeControllerを持っていると仮定しますDashboardController、両方が自分の意見に表示する通知を持っている

home/index.html.erb

<div id='mynotificationsdiv'></div> 

<%= render 'shared/notifications' %> 

「ダッシュボード/ index.html.erb」

<div id='mynotificationsdiv'></div> 

<%= render 'shared/notifications' %> 
+0

大丈夫すでにコントローラから新しいレコードを取得しているので、すべてのページでコントローラを呼び出すとどうなりますか?すべてのコントローラにコードを書く必要がありますか、application_controllerにコードを書きますか? – Ray

+0

いいえ、コントローラの内部に入るコードを繰り返す必要はありません。 jqueryをRailsアプリケーションのjsライブラリとして使用している場合は、http://api.jquery.com/jquery.ajax/でajaxリクエストを実行する方法を確認してください。 簡単にあなたの意見からそれを呼び出してください!また、通知の呼び出しでビューの部分を使用するか、ajaxの呼び出しをデータ属性にバインドしてHTMLを再利用できるため、コードを複製する必要はありません。 – Codextremist

+0

Ok @Ray私はあなたのアイデアを得ることができるように答えを編集しましたか? – Codextremist

関連する問題