[如果我的应用使用AJAX将用户的message
提交到不需要控制器的服务器端speak
方法(因此也没有空间写入before_filter
),那么我将如何实现限制[ users
谁不属于通过浏览器控制台提交消息的那个conversation
?
如果我打开我的应用程序并键入App.messages.speak("hi", #guess random number#)
,则无论消息是否属于该消息,都会将该消息提交到对话中。>>
messages_channel.rb
class MessagesChannel < ApplicationCable::Channel def speak(data) Message.create! body: data['body'], conversation_id: data['conversation_id'], user_id: current_user.id end end
messages.coffee
App.messages = App.cable.subscriptions.create "MessagesChannel", speak: (body, conversation_id) -> @perform 'speak', body: body, conversation_id: conversation_id submit_message = () -> $('#response').on 'keydown', (event) -> if event.keyCode is 13 conversation_id = $("[data-conversation-id]").data("conversation-id") App.messages.speak(event.target.value, conversation_id) event.target.value = "" event.preventDefault()
我以为我可以从
conversation_id
方法中删除speak
参数,并将其设置在不依赖用户输入的其他位置?目前,我已将其设置为数据属性,这意味着人们仍然可以使用它。
如果我的应用程序使用AJAX将用户的消息提交到不需要控制器的服务器端语音方法(因此也没有空间来编写before_filter,那么我将如何实现......>
class Message < ApplicationRecord
before_create :exclude_non_participants
def exclude_non_participants
if conversation.participates?(user)
true
else
throw :abort
end
end
end