Rails 5: ActionCable doesn't save messages into db









up vote
-1
down vote

favorite












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









share|improve this question























  • Please post your connection.rb file
    – fool-dev
    yesterday










  • Added connection.rb file.
    – klasarn
    yesterday














up vote
-1
down vote

favorite












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









share|improve this question























  • Please post your connection.rb file
    – fool-dev
    yesterday










  • Added connection.rb file.
    – klasarn
    yesterday












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











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









share|improve this question















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






ruby-on-rails ruby-on-rails-5 actioncable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday

























asked yesterday









klasarn

106




106











  • Please post your connection.rb file
    – fool-dev
    yesterday










  • Added connection.rb file.
    – klasarn
    yesterday
















  • Please post your connection.rb file
    – fool-dev
    yesterday










  • Added connection.rb file.
    – klasarn
    yesterday















Please post your connection.rb file
– fool-dev
yesterday




Please post your connection.rb file
– fool-dev
yesterday












Added connection.rb file.
– klasarn
yesterday




Added connection.rb file.
– klasarn
yesterday












1 Answer
1






active

oldest

votes

















up vote
0
down vote













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






share|improve this answer




















  • 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
    yesterday










  • 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
    yesterday










  • You can follow the repo for refactoring your consept, which I post in my answer
    – fool-dev
    yesterday










  • 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
    yesterday










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224169%2frails-5-actioncable-doesnt-save-messages-into-db%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













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






share|improve this answer




















  • 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
    yesterday










  • 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
    yesterday










  • You can follow the repo for refactoring your consept, which I post in my answer
    – fool-dev
    yesterday










  • 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
    yesterday














up vote
0
down vote













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






share|improve this answer




















  • 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
    yesterday










  • 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
    yesterday










  • You can follow the repo for refactoring your consept, which I post in my answer
    – fool-dev
    yesterday










  • 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
    yesterday












up vote
0
down vote










up vote
0
down vote









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






share|improve this answer












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







share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









fool-dev

5,56272440




5,56272440











  • 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
    yesterday










  • 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
    yesterday










  • You can follow the repo for refactoring your consept, which I post in my answer
    – fool-dev
    yesterday










  • 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
    yesterday
















  • 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
    yesterday










  • 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
    yesterday










  • You can follow the repo for refactoring your consept, which I post in my answer
    – fool-dev
    yesterday










  • 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
    yesterday















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
yesterday




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
yesterday












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
yesterday




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
yesterday












You can follow the repo for refactoring your consept, which I post in my answer
– fool-dev
yesterday




You can follow the repo for refactoring your consept, which I post in my answer
– fool-dev
yesterday












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
yesterday




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
yesterday

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224169%2frails-5-actioncable-doesnt-save-messages-into-db%23new-answer', 'question_page');

);

Post as a guest














































































Popular posts from this blog

Kleinkühnau

Makov (Slowakei)

Deutsches Schauspielhaus