2016-11-20 83 views
0

我正在學習Enough Action Cable教程。我已經到了第6部分的末尾,添加了@mentions來爲提到的用戶創建通知。其中一項練習是將發送用戶的姓名附加到警報文本中。到目前爲止,我只獲得「@undefined」。我猜data.mention.username不是正確的調用附加。在控制檯從消息中拉出用戶名我做了User.find_by(id:Message.last.user_id).username,但我不知道如何將其轉換爲正在運行的coffeescript。消息用戶名未定義爲ActionCable聊天應用程序

room.coffee

App.room = App.cable.subscriptions.create "RoomChannel", 
    connected: -> 
# Called when the subscription is ready for use on the server 
    disconnected: -> 
# Called when the subscription has been terminated by the server 
    received: (data) -> 
alert("You have a new mention from @" + data.mention.username) if data.mention 
if (data.message && !data.message.blank?) 
    $('#messages-table').append data.message 
    scroll_bottom() 
$(document).on 'turbolinks:load', -> 
    submit_message() 
    scroll_bottom() 
submit_message =() -> 
    $('#message_content').on 'keydown', (event) -> 
if event.keyCode is 13 && !event.shiftKey 
    $('input').click() 
    event.target.value = "" 
    event.preventDefault() 
scroll_bottom =() -> 
    $('#messages').scrollTop($('#messages')[0].scrollHeight) 

messages_controller.rb

class MessagesController < ApplicationController 
    before_action :logged_in_user 
    before_action :get_messages 

    def index 
    end 

    def create 
    message = current_user.messages.build(message_params) 
    if message.save 
     ActionCable.server.broadcast 'room_channel', 
      message: render_message(message) 
     message.mentions.each do |mention| 
     ActionCable.server.broadcast "room_channel_user_#{mention.id}", 
      mention: true 
     end 
    end 
    end 


    private 

    def get_messages 
     @messages = Message.for_display 
     @message = current_user.messages.build 
    end 

    def message_params 
     params.require(:message).permit(:content) 
    end 

    def render_message(message) 
     render(partial: 'message', locals: { message: message }) 
    end 

end 

message.rb

class Message < ApplicationRecord 
    belongs_to :user 
    validates :content, presence: true 
    scope :for_display, -> { order(:created_at).last(50) } 

    # Returns a list of users @mentioned in message content. 
    def mentions 
    content.scan(/@(#{User::NAME_REGEX})/).flatten.map do |username| 
     User.find_by(username: username) 
    end.compact 
    end 
end 

room_channel.rb

class RoomChannel < ApplicationCable::Channel 
    def subscribed 
    stream_from "room_channel" 
    stream_from "room_channel_user_#{message_user.id}" 
    end 

    def unsubscribed 
    # Any cleanup needed when channel is unsubscribed 
    end 
end 

回答

0

必須在消息控制器的create方法中定義用戶名。因此,在messages_controller.rb的創建方法看起來是這樣的:

def create 
    message = current_user.messages.build(message_params) 
    if message.save 
     ActionCable.server.broadcast 'room_channel', 
      message: render_message(message) 
     message.mentions.each do |mention| 
     ActionCable.server.broadcast "room_channel_user_#{mention.id}", 
      mention: true, 
      origin: "@#{message.user.username}" 
     end 
    end 
    end 

而且room.coffee警告稱data.origin像這樣:

received: (data) -> 
    alert("You have a new mention from " + data.origin) if data.mention 
    if (data.message && !data.message.blank?) 
     $('#messages-table').append data.message 
     scroll_bottom() 

由於LearnEnough學會LIUSINING指着我的正確的方向。

相關問題