javascript - Unable to restrict redirection of page on File Upload in rails 5.0.0? -
i uploading file in chat application, on upload redirecting page. have tried in many ways restrict failed.
please me valuable response.
attachment model:
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
attachment 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
attachment view:
<%= form_for @attachment , url: "/chats/#{@chat.id}/attachments", :remote => true, authenticity_token: true, html: { multipart: true } |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 updating on front-end:(using 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>"; } })
edit - 1
in front end updating as
<% 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 %>
attachment parital
<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 @ %i:%m%p") %>"> <%= message_interlocutor(attachment).name %> • <%= attachment.created_at.strftime("%h:%m %p") %> </time> </div> </li>
i suspect reason getting redirected because not specifying how respond different request formats. need see server log somethings along lines of:
started "/chats/#{@chat.id}/attachments" 127.0.0.1 @ 2016-09-13 15:38:23 +0900 processing rails::attachmentscontroller#create html #<=
you must specify how respond different request formats such html
or js
. form using remote: true
param should js
request(ajax
).
def create respond_to |format| format.js @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
let me know if problem persists.
edit#1
when using respond_to
method, must specify html
format reason.
respond_to |format| format.js ... end format.html { ... } end
if need default behavior html
, can use:
format.html
Comments
Post a Comment