2017-03-03 37 views
0

在我的ruby on rails應用程序中,我在每個頁面中都有標​​準視圖,它在每個頁面上呈現導航視圖,但是我有一個通知控制器,我希望在每個頁面上使用,因此理論上我需要加載這個控制器和視圖每一頁上這是可能的Mutilple控制器在一個頁面上

<%= 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

你所有的服務器端網頁是由每一個時間控制器渲染。這是導軌工作的方式。 然後,一旦你的頁面已經被加載,你可以使用客戶端代碼(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

好吧我有控制器上的Ajax已經從數據庫中獲取新記錄,所以我怎麼會去調用每個頁面中的控制器,我是否需要在每個控制器中編寫代碼,或者我應該將代碼寫入application_controller中? – Ray

+0

不,你不需要重複控制器內部的代碼。如果您在Rails應用程序中使用jquery作爲js庫,請檢查如何在此處執行ajax請求:http://api.jquery.com/jquery.ajax/。 只需從您的意見中調用它!而且你不需要複製代碼,因爲你可以在通知調用中使用視圖partials,或者將ajax調用綁定到data-attributes併爲此重用HTML。 – Codextremist

+0

好@Ray我編輯了答案,這樣你就可以明白了嗎? – Codextremist

相關問題