我正在聊天应用程序中上传文件,但在上传时会重定向页面。我试过很多方法限制它,但我失败了。
请帮助我做出有价值的回应。
附件型号:
<?php
$regex = '~(<(\w+).+</\2>)~';
$string = 'crazy stuff<a href="https://websta.me/p/1337373806024694030_327078936"><img src="https://scontent.cdninstagram.com/t51.2885-15/e15/14287934_1389514537744146_673363238_n.jpg?ig_cache_key=MTMzNzM3MzgwNjAyNDY5NDAzMA%3D%3D.2"></a>here as well';
if (preg_match($regex, $string, $match)) {
echo $match[1];
}
?>
附件控制器:
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
附件视图
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
在前端更新的Javascript :(使用Rails ActionCable)
<%= 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 %>
编辑 - 1
在前端我正在更新为
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>";
}
})
附件公寓
<% 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 %>
答案 0 :(得分:1)
我怀疑您被重定向的原因是因为您没有指定如何响应不同的请求格式。我需要看到你的服务器日志中的某些内容:
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
您必须指定如何回复不同的请求格式,例如HTML
或JS
。您的表单正在使用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
如果问题仍然存在,请告诉我。
EDIT#1
使用respond_to
方法时,您还必须出于某种原因指定HTML
格式。
respond_to do |format|
format.js do
...
end
format.html { ... }
end
如果您只需要HTML
的默认行为,则可以使用:
format.html
答案 1 :(得分:1)
在控制器文件中删除 head: ok
。它没有重定向。
干杯。