Ruby on rails Rails 5:ActionCable不';不要将消息保存到数据库中
我目前面临的问题是,我的邮件没有保存到数据库中。因为我使用sqlite3,所以我认为这个问题应该在我的设置中解决 但肯定还有别的东西。。。当我启动服务器时,命令提示符显示Ruby on rails Rails 5:ActionCable不';不要将消息保存到数据库中,ruby-on-rails,ruby-on-rails-5,actioncable,Ruby On Rails,Ruby On Rails 5,Actioncable,我目前面临的问题是,我的邮件没有保存到数据库中。因为我使用sqlite3,所以我认为这个问题应该在我的设置中解决 但肯定还有别的东西。。。当我启动服务器时,命令提示符显示MessagesChannel正在从对话\uuu流式传输,而应该是conversation\u1 另外,当我尝试发送消息时,命令提示符会显示以下内容 No template found for MessagesController#create rendering head :no_content 我的消息\u channel
MessagesChannel正在从对话\uuu流式传输
,而应该是conversation\u1
另外,当我尝试发送消息时,命令提示符会显示以下内容
No template found for MessagesController#create rendering head :no_content
我的消息\u channel.rb
class MessagesChannel < ApplicationCable::Channel
def subscribed
stream_from "conversation_#{params[:id]}"
end
end
消息控制器
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
class messages控制器
邮件视图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
模块应用程序表
类连接
哦!您已错过连接,因此应按如下方式配置连接文件
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
模块应用程序表
类连接
这是我制作的作弊应用程序请发布你的
connection.rb
文件added connection.rb文件。谢谢,但这不起作用。我仍然收到相同的错误,消息没有保存到数据库中。它仍然是从对话流出来的,好吧,别泄气,我想问题出在哪里了,但第一步是在工作前配置连接,以便采取行动。你可以按照repo重构你的概念,这是我在回答中发布的。我试着调试了一点应用程序,问题仍然存在,但目前只有第一条记录保存到数据库中。第一条消息之后的每条消息都不会保存。它总是引用消息_controller.rb,因为render和redirect _to是在同一个方法中调用的。所以我删除了重定向到,但我仍然面临记录没有保存到数据库中的问题。。。
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