2016-11-20 6 views
0

私はLearn Enough Action Cableチュートリアルに従っています。セクション6の最後に、@ mentionsを追加して、言及されたユーザーに通知を作成しました。練習の1つは、送信ユーザーの名前をアラートテキストに追加することです。これまでのところ、私は "@undefined"しか得ていません。私はdata.mention.usernameが追加する正しい呼び出しではないと推測しています。コンソールから私はUser.find_by(id:Message.last.user_id).usernameを実行しましたが、私はそれを作業中のcoffeescriptにどのように翻訳するのか分かりません。ActionCableチャットアプリのメッセージusername undefined

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

メッセージ・コントローラーの作成メソッド内でユーザー名を定義する必要があります。

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呼び出します:だからのようになりますmessages_controller.rbでメソッドを作成して私を指し示すLearnEnough学会LIUSININGに

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

感謝右方向。

関連する問題