Ray
Ray

Reputation: 123

Mutilple controllers on one page

In my ruby on rails app i have the standard view in each page which renders a navigation view on each page, but i have a notifications controller which i wish to use on every page so theoretically i need to load this controller and view on every page is this possible

this <%= render 'shared/navigation' %> renders a view so no use what im attempting to do i run a controller inside another controller basically

thanks for any help

ps. i have had a good look around and cannot find anything that suites what im trying to do

Notifications controller code

  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

Upvotes: 0

Views: 62

Answers (1)

Codextremist
Codextremist

Reputation: 131

All your server side page is rendered by one controller per time. This is the way rails works. Then, once your page has been loaded, you can use client-side code (javascript), to fetch notifications from your "NotificationsController". This is actually a pretty common pattern, and a good way to solve your problem. Imagine that new notifications arise while the user is checking the page. With a simple ajax code, polling the "NotificationsController" every X seconds, you can even have notifications printed out to the user while they arrive

An example: shared/_notifications.html.erb

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

Now suppose you have a HomeController and DashboardController, both have notifications to display in their views

home/index.html.erb

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

<%= render 'shared/notifications' %>

'dashboard/index.html.erb'

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

<%= render 'shared/notifications' %>

Upvotes: 2

Related Questions