2017-03-03 107 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

相关问题