在我的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
答案 0 :(得分:2)
所有服务器端页面每次都由一个控制器呈现。这是rails的工作方式。 然后,一旦您的页面被加载,您就可以使用客户端代码(javascript)从您的&#34; NotificationsController&#34;中获取通知。这实际上是一种非常常见的模式,也是解决问题的好方法。 想象一下,当用户检查页面时会出现新的通知。使用简单的ajax代码,轮询&#34; NotificationsController&#34;每隔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' %>
&#39;仪表板/ index.html.erb&#39;
<div id='mynotificationsdiv'></div>
<%= render 'shared/notifications' %>