Rails 5:ActionCable不会将消息保存到数据库中

时间:2018-11-09 10:42:33

标签: ruby-on-rails ruby-on-rails-5 actioncable

我目前面临的问题是我的邮件未保存到数据库中。我认为问题出在我的设置中,因为我使用了sqlite3。

但是必须有其他内容...当我启动服务器时,命令提示符会显示MessagesChannel is streaming from conversation_而不是conversation_1

当我尝试发送消息时,命令提示符也会显示以下内容

No template found for MessagesController#create rendering head :no_content

我的messages_channel.rb

class MessagesChannel < ApplicationCable::Channel
  def subscribed
    stream_from "conversation_#{params[:id]}"
  end
end

我的邮件。咖啡

$(() ->
App.messages = App.cable.subscriptions.create {channel: 'MessagesChannel', id: $('#conversation_id').val() },
  received: (data) ->
    $('#new_message')[0].reset()
    $('#chat').prepend data.message
)

MessagesController

class MessagesController < ApplicationController
  before_action :authenticate_user!
  before_action :set_conversation

  def index
    if current_user == @conversation.sender || current_user == @conversation.recipient
      @other = current_user == @conversation.sender ? @conversation.recipient : @conversation.sender
      @messages = @conversation.messages.order("created_at DESC")
    else
      redirect_to conversations_path, alert: "You don't have permission."
    end
  end

  def create
    @message = @conversation.messages.new(message_params)
    @messages = @conversation.messages.order("created_at DESC")

    if @message.save
      ActionCable.server.broadcast "conversation_#{@conversation.id}", message: render_message(@message)
      redirect_to conversation_messages_path(@conversation)
    end
  end


  private

  def render_message(message)
    self.render(partial: 'messages/message', locals: {message: message})
  end

  def set_conversation
    @conversation = Conversation.find(params[:conversation_id])
  end

  def message_params
    params.require(:message).permit(:context, :user_id)
  end
end

消息视图index.html.erb

<div class="container-small">
  <div class="row">
    <div class="col-md-3 text-center">
      <%= image_tag avatar_url(@other), class: "img-circle avatar-medium" %>
      <strong><%= @other.fullname %></strong>
      <%= link_to "Profil Ansehen", @other, class: "btn btn-default" %>
    </div>
    <div class="col-md-9">
      <div class="panel panel-default">
        <div class="panel-heading">
          Chat mit <%= @other.fullname %>
          <input id="conversation_id" type="hidden" value="<%= @conversation.id %>">
        </div>
        <div class="panel-body">
          <div class="container text-center">
            <%= form_for [@conversation, @conversation.messages.new], remote: true do |f| %>
              <div class="form-group">
                <%= f.text_field :context, placeholder: "Deine Nachricht", class: "form-control" %>
              </div>
              <%= f.hidden_field :user_id, value: current_user.id %>
              <div>
                <%= f.submit "Absenden", class: "btn btn-normal" %>
              </div>
            <% end %>
          </div>
        </div>
      </div>
        <div id="chat">
          <%= render @messages %>
        </div>
    </div>
  </div>
</div>

Connection.rb

module ApplicationCable
  class Connection < ActionCable::Connection::Base
  end
end

1 个答案:

答案 0 :(得分:0)

哦!您错过了连接,因此应将连接文件配置为

module ApplicationCable
    class Connection < ActionCable::Connection::Base
        identified_by :current_user

        def connect
            self.current_user = find_verified_user
        end

        protected

        def find_verified_user
            if (current_user = env['warden'].user) //Devise authentication
                current_user
            else
                reject_unauthorized_connection
            end
        end
    end
end

这是我制作的作弊应用see this