DoubleRenderError(渲染和/或重定向,并在redirect_to或self.render的末尾添加“和return" [英] DoubleRenderError (Render and/or redirect and adding 'and return' at the end of redirect_to or self.render
问题描述
每次将消息插入数据库时,我都有一个消息传递系统.它会插入重复的代码,从而获取 AbstractController :: DoubleRenderError(此操作多次调用了渲染和/或重定向)
.我尝试在redirect_to或 self.render
的末尾添加 并返回
,但这没有帮助.似乎是通过create方法出现的问题
I have a messaging system every time I insert a message into the database. it insert duplicates im getting AbstractController::DoubleRenderError (Render and/or redirect were called multiple times in this action)
. I tried to add and return
at the end of redirect_to or self.render
but it did not help. the problem it seems to be happening from the create method
# app/controllers/messages_controller.rb
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 to view this."
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
通过删除
ActionCable.server.broadcast "conversation_#{@conversation.id}", message: render_message(@message)
redirect_to conversation_messages_path(@conversation)
它仍会插入重复的邮件
def create
@message = @conversation.messages.new(message_params)
@messages = @conversation.messages.order("created_at DESC")
@message.save
end
这是视图app/views/messages/index.html.erb
this is the view app/views/messages/index.html.erb
<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 "View Profile", @other, class: "btn btn-default" %>
</div>
<div class="col-md-9">
<div class="panel panel-default">
<div class="panel-heading">
Conversation with <%= @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: "Add a personal message", class: "form-control" %>
</div>
<%= f.hidden_field :user_id, value: current_user.id %>
<div>
<%= f.submit "Send Message", class: "btn btn-normal" %>
</div>
<% end %>
</div>
</div>
</div>
<div id="chat">
<%= render @messages %>
</div>
推荐答案
您可以在create方法中调用 render
和 redirect_to
:
You call both render
and redirect_to
in the create method:
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) # <------ render here
redirect_to conversation_messages_path(@conversation) # <-- redirect
end
end
private
def render_message(message)
self.render(partial: 'messages/message', locals: {message: message})
end
您只能调用其中之一-我建议删除 render_message
或避免使用 render
方法.如果要在创建后显示消息,可以使用 Flash .
you can call only one of them - I'd advise to remove render_message
or avoid using there render
method. If you want to show a message after creation, you can use Flash.
这篇关于DoubleRenderError(渲染和/或重定向,并在redirect_to或self.render的末尾添加“和return"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!