在我的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