gpt4 book ai didi

javascript - 无法在 Rails 5.0.0 中限制文件上传页面的重定向?

转载 作者:行者123 更新时间:2023-12-02 14:11:55 25 4
gpt4 key购买 nike

我正在聊天应用程序中上传文件,但上传时会重定向页面。我尝试过很多方法来限制它,但都失败了。

请帮助我提供宝贵的回复。

附件模型:

class Attachment < ApplicationRecord
belongs_to :user
belongs_to :chat

validates_presence_of :user_id , :chat_id

has_attached_file :attachment
validates_attachment_content_type :attachment, content_type: /.*/
end

附件 Controller :

class AttachmentsController < ApplicationController
before_action :logged_in_user
layout false

def create
@chat = Chat.find(params[:chat_id])
@attachment = @chat.attachments.build(attachment_params)
@attachment.user_id = current_user.id
if @attachment.save
ActionCable.server.broadcast 'messages',
message: @attachment.attachment,
user: @attachment.user.name,
action: "attachment"
head :ok
end
end

private
def attachment_params
params.require(:post).permit(:attachment)
end
end

附件 View :

<%= form_for @attachment  , url: "/chats/#{@chat.id}/attachments", :remote => true,  authenticity_token: true, html: { multipart: true } do |f| %>

<%= f.hidden_field :chat_id, value: @chat.id %>

<input type="file" name="post[attachment]" onchange="this.form.submit();return false;" id="message_attachment" type="file">

<% end %>

用于更新前端的Javascript:(使用Rails ActionCable)

App.messages = App.cable.subscriptions.create('MessagesChannel',{
received: function(data) {
$('#new-messages').removeClass('hidden');
if (data.action == "attachment") {
return $('#new-messages').append(this.renderAttachment(data));
}
},
renderAttachment: function(data) {
return data.message //"<li class='<%= self_or_other(data)%>'><div class='chatboxmessagecontent'><p>" + data.message + "</p>" + data.user + " • " + "</div></li>";
}
})

编辑 - 1

在前端我更新为

<% if @attachments.any? %>              
<div id="messages">
<%= render partial: 'attachments/attachment', collection: @attachments %>
</div>
<div class="hidden" id="new-messages"></div>
<span id="istyping"></span>
<% else %>
<div class="hidden" id="new-messages"></div>
<% end %>

附件部分

<li class="<%=  self_or_other(attachment) %>">
<div class="chatboxmessagecontent">

<a href="<%= attachment.attachment.url %>" download>
<%= image_tag attachment.attachment.url, height: '64', width: '64' %>
</a><br/>

<time datetime="<%= attachment.created_at %>" title="<%= attachment
.created_at.strftime("%d %b %Y at %I:%M%p") %>">
<%= message_interlocutor(attachment).name %> • <%= attachment.created_at.strftime("%H:%M %p") %>
</time>

</div>
</li>

最佳答案

我怀疑您被重定向的原因是因为您没有指定如何响应不同的请求格式。我需要查看您的服务器日志以了解以下内容:

Started GET "/chats/#{@chat.id}/attachments" for 127.0.0.1 at 2016-09-13 15:38:23 +0900

Processing by Rails::AttachmentsController#create as HTML #<= this

您必须指定如何响应不同的请求格式,例如 HTMLJS。您的表单使用 remote: true 参数,因此它应该是 JS 请求(ajax)。

def create
respond_to do |format|
format.js do
@chat = Chat.find(params[:chat_id])
@attachment = @chat.attachments.build(attachment_params)
@attachment.user_id = current_user.id
if @attachment.save
broadcast_attachment(@attachment)
return head :no_content
end
end
end
end

def broadcast_attachment(attachment)
ActionCable.server.broadcast 'messages', message: attachment.attachment,
user: attachment.user.name,
action: 'attachment'
end

如果问题仍然存在,请告诉我。

编辑#1

使用 respond_to 方法时,由于某种原因,您还必须指定 HTML 格式。

respond_to do |format|
format.js do
...
end
format.html { ... }
end

如果您只需要 HTML 的默认行为,则只需使用:

format.html

关于javascript - 无法在 Rails 5.0.0 中限制文件上传页面的重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39462871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com