-1

I'm currently facing the issue that my messages aren't saved into the database. I thought to problem would lie within in my setup, since I use sqlite3.

But there must be something else... When I start my server the command prompt says MessagesChannel is streaming from conversation_ instead it should conversation_1

Also when I try to send a message the command prompt says the following

No template found for MessagesController#create rendering head :no_content

My messages_channel.rb

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

My messages.coffee

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

messages view 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
klasarn
  • 129
  • 1
  • 12

1 Answers1

0

Ohh! you have missed the connection so you should configure the connection file as like

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

here is I have made a cheating app see this

fool-dev
  • 7,671
  • 9
  • 40
  • 54
  • Thank you, but this doesn't work. I'm still getting the same error and the message isn't saved into the db. It's still streaming from conversation_ – klasarn Nov 09 '18 at 11:27
  • OK, don't frustrate I think the problem has somewhere but the first stage is to configure the connection before work for action cable – fool-dev Nov 09 '18 at 11:29
  • You can follow the repo for refactoring your consept, which I post in my answer – fool-dev Nov 09 '18 at 11:31
  • I managed to debug the app a little, the problem still occurs but right now only the first record saves into the db. Every message after the first one isn't saved. It always refers to the messages_controller.rb since render and redirect_to are called in the same method. So I delete the redirect_to but I'm still facing the issue that records aren't saved into the db... – klasarn Nov 09 '18 at 14:42