0
我是初學者的鐵軌。我正在構建一個論壇應用程序。它有一個消息傳遞工具,用戶可以私下向其他用戶發送消息(不是實時的,就像通知一樣)。我已經完成了這個。但我想添加阻止功能,用戶可以阻止其他用戶,以避免從這些特定用戶獲取消息。我怎樣才能做到這一點?我感謝你的回答。提前致謝。
這是我的代碼。
通知控制器如何阻止用戶獲取消息?
class NotificationsController < ApplicationController
layout "posts"
after_action :read_message, only: [:index]
def index
@notifications = Notification.where(:recipient_id => session[:registered_id]).order("created_at DESC")
end
def new
@user = User.find(session[:user_id])
@notification = @user.notifications.new
end
def create
@user = User.find(session[:user_id])
@notification = @user.notifications.new notification_params
if @notification.save
redirect_to(:controller => "posts", :action => "index")
else
render "new"
end
end
def sent_messages
@notifications = Notification.where(:user_id => session[:user_id]).order("created_at DESC")
end
private
def notification_params
params.require(:notification).permit(:message, :user_id, :recipient_id, :status)
end
def read_message
@notifications = Notification.where(:recipient_id => session[:registered_id]).order("created_at DESC")
@notifications.read_all
end
end
通知模型
class Notification < ActiveRecord::Base
belongs_to :user
validates :message, :presence => true
validates :recipient_id, :presence => true
def self.read_all
Notification.all.update_all(status: true)
end
end
通知遷移
class CreateNotifications < ActiveRecord::Migration
def change
create_table :notifications do |t|
t.text :message
t.integer :user_id
t.string :recipient_id
t.boolean :read, default: false
t.references :user, index: true, foreign_key: true
t.timestamps null: false
end
end
end
**通知#指數**
<div id = "messages_wrapper">
<% @notifications.each do |notification| %>
<div class="<%= notification.status ? 'message_wrapper_read' : 'message_wrapper_unread' %>">
<p><%= notification.message %></p>
<% if notification.user_id %>
<p class = "message_details">from <span><%= notification.user.registered_id %></span></p>
<% end %>
</div>
<% end %>
</div>
你問我們爲您設計和實現一個功能,這是堆棧溢出的範圍之內。您需要嘗試自己編寫此功能,然後詢問您何時遇到無法自行解決的特定問題。我可以告訴你,你需要一個表格映射阻止者到被阻止者(都是user_id的外鍵),剩下的應該是相對容易的。 – MarsAtomic