如何阻止用户收到邮件?

时间:2016-01-11 20:16:25

标签: ruby-on-rails ruby ruby-on-rails-4

我是初学者。我正在构建一个论坛应用程序。它有一个消息传递工具,用户可以私下向其他用户发送消息(不是实时消息。就像通知一样)。我已经实现了这一点。但我想添加阻止功能,用户可以阻止其他用户,以避免从这些特定用户获取消息。我怎样才能做到这一点?我感谢你的回答。在此先感谢。

这是我的代码。
通知控制器

 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

**通知#index **

<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>

1 个答案:

答案 0 :(得分:1)

对于被阻止的用户概念,您可以在用户模型中添加一个名为blocked_users的自定义属性,该属性在数据库中存储为数组。

对于postgresql,您可以使用数组数据类型。

notification.rb文件中,

#Validations,
validate :is_not_blocked_by_recipient

def is_not_blocked_by_recipient
  #Check if the user is blocked or no, and write the logic
  #self.errors.add()
end

应该有效