应用中的通知系统
notification system in application
我是 ruby rails 的初学者。我正在构建一个论坛应用程序。除了 post 和评论 post 之外,我还想要应用程序中的私人消息系统。该消息应发送给所需的用户 ("only that user can see the message")。为此,我生成了一个模型 Notification,其中包含 message 作为列。
通知模型
class Notification < ActiveRecord::Base
belongs_to :user
end
通知迁移
class CreateNotifications < ActiveRecord::Migration
def change
create_table :notifications do |t|
t.text :message
t.timestamps null: false
t.references :user, index: true, foreign_key: true
end
end
end
通知控制器
class NotificationsController < ApplicationController
def index
@notifications = Notification.all.order("created_at DESC")
end
def new
@notification = Notification.new
end
def create
@notification = Notification.new notification_params
if @notification.save
redirect_to(:controller => "posts", :action => "index")
else
render "new"
end
end
private
def notification_params
params.require(:notification).permit(:message, :user_id)
end
end
通知#new 查看
<%= form_for(:notification, :url => {:action => "create"}) do |f| %>
<%= f.text_field(:message, :placeholder => "Enter your message") %>
<%= f.hidden_field(:user_id, :value => session[:user_id]) %>
<%= f.submit("send message") %>
<% end %>
如何在表单中发送目标用户属性?还是消息系统有不同的方式?
请帮助我。感谢您的回答。提前致谢。
您需要在通知中存储对收件人的引用。
t.integer :recipient_id
class Notification < ActiveRecord::Base
belongs_to :recipient, class_name: 'User'
end
也不要在表单中设置user_id,在控制器中设置。
(否则任何人都可以为其他人创建通知,只需发送错误的输入即可)。
class NotificationsController < ApplicationController
def create
@user = User.find(session[:user_id])
@notification = @user.notifications.new notification_params
# ...
end
end
不是完整的解决方案,但会为您指明正确的方向。
我是 ruby rails 的初学者。我正在构建一个论坛应用程序。除了 post 和评论 post 之外,我还想要应用程序中的私人消息系统。该消息应发送给所需的用户 ("only that user can see the message")。为此,我生成了一个模型 Notification,其中包含 message 作为列。
通知模型
class Notification < ActiveRecord::Base
belongs_to :user
end
通知迁移
class CreateNotifications < ActiveRecord::Migration
def change
create_table :notifications do |t|
t.text :message
t.timestamps null: false
t.references :user, index: true, foreign_key: true
end
end
end
通知控制器
class NotificationsController < ApplicationController
def index
@notifications = Notification.all.order("created_at DESC")
end
def new
@notification = Notification.new
end
def create
@notification = Notification.new notification_params
if @notification.save
redirect_to(:controller => "posts", :action => "index")
else
render "new"
end
end
private
def notification_params
params.require(:notification).permit(:message, :user_id)
end
end
通知#new 查看
<%= form_for(:notification, :url => {:action => "create"}) do |f| %>
<%= f.text_field(:message, :placeholder => "Enter your message") %>
<%= f.hidden_field(:user_id, :value => session[:user_id]) %>
<%= f.submit("send message") %>
<% end %>
如何在表单中发送目标用户属性?还是消息系统有不同的方式?
请帮助我。感谢您的回答。提前致谢。
您需要在通知中存储对收件人的引用。
t.integer :recipient_id
class Notification < ActiveRecord::Base
belongs_to :recipient, class_name: 'User'
end
也不要在表单中设置user_id,在控制器中设置。 (否则任何人都可以为其他人创建通知,只需发送错误的输入即可)。
class NotificationsController < ApplicationController
def create
@user = User.find(session[:user_id])
@notification = @user.notifications.new notification_params
# ...
end
end
不是完整的解决方案,但会为您指明正确的方向。