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
ruby-on-rails ruby-on-rails-5 actioncable
add a comment |
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
ruby-on-rails ruby-on-rails-5 actioncable
Please post yourconnection.rbfile
– fool-dev
yesterday
Added connection.rb file.
– klasarn
yesterday
add a comment |
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
ruby-on-rails ruby-on-rails-5 actioncable
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
ruby-on-rails ruby-on-rails-5 actioncable
edited yesterday
asked yesterday
klasarn
106
106
Please post yourconnection.rbfile
– fool-dev
yesterday
Added connection.rb file.
– klasarn
yesterday
add a comment |
Please post yourconnection.rbfile
– 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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Please post your
connection.rbfile– fool-dev
yesterday
Added connection.rb file.
– klasarn
yesterday