在我的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
到在同一頁上呈現來自「多個控制器」的內容的唯一方式是通過使用AJAX.The主控制器呈現頁面服務器端,另一組塊頁面由AJAX渲染(擊中其他控制器)。我不知道我真的明白你在做什麼。你使用多個控制器意味着什麼? – Codextremist
好吧,我已經爲通知頁面做了一個控制器,但是我想在每個頁面上的下拉框中進行通知 – Ray
我想你應該將通知控制器方法移動到應用程序控制器,並在每個操作之前使用過濾器來調用它。視圖代碼可以使用應用程序佈局文件中的部分進行渲染。 – webster