RubyのRubyアプリケーションでは、各ページにナビゲーションビューをレンダリングする標準ビューがありますが、すべてのページで使用したい通知コントローラがあります。すべてのページにこのコントローラとビューをロードすると、これが可能1ページに無制限のコントローラ
この<%= 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