一个页面上的多个控制器

Mutilple controllers on one page

在我的 ruby on rails 应用程序中,我在每个页面中都有标​​准视图,它在每个页面上呈现一个导航视图,但我有一个通知控制器,我希望在每个页面上使用它所以理论上我需要加载这个控制器并在每个页面上查看这是否可能

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

您所有的服务器端页面每次都由一个控制器呈现。这就是 rails 的工作方式。 然后,一旦您的页面加载完毕,您就可以使用客户端代码 (javascript) 从您的 "NotificationsController" 获取通知。这实际上是一种非常常见的模式,也是解决问题的好方法。 想象一下,当用户检查页面时出现新的通知。使用简单的 ajax 代码,每隔 X 秒轮询 "NotificationsController",您甚至可以在用户到达时向他们打印通知

一个例子: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' %>

'dashboard/index.html.erb'

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

<%= render 'shared/notifications' %>