Can I redirect a javascript request to another javascript action?

I have a comment controller with an index and create actions among others. Both of these actions correspond to the html and js formats.

Whenever I get a create request via ajax, I would like to add a new comment and then redirect to index.js, so the comments on the screen are updated without reloading the page.

A similar thing works in Chrome. But whenever I try to do this in Firefox or IE, it turns out that the redirect from create.js gets into index.html ...

Even when I force js redirect:

redirect_to polymorphic_path([@commentable, :comments]), :format => 'js'

It gets into the .html format in Firefox and IE.

Any idea what could be happening here?

+3
1

, 302 -. , ( : http://trac.tools.ietf.org/wg/httpbis/trac/ticket/160).

, , URL- JS, . - :

class CommentsController < ApplicationController
  def index 
    setup_for_index
    respond_to :html, :js
  end

  def create
    # Creation stuff...
    respond_to do |format|
      format.html {redirect_to :action => :index}
      format.js do
        setup_for_index
        render :action => :index
      end
    end
  end

  private

  def setup_for_index
    @comments = ...
  end
end
+1

Source: https://habr.com/ru/post/1762294/


All Articles