chell
chell

Reputation: 7866

render html instead of js for an ajax request in Rails

I have the following controller action:

  def create
     @board = Board.new(params[:board])
      respond_to do |format|
        if @board.save
          set_board_session @board
          set_new_board
           format.js   { render :action => "show" } #<== here I want to render html no js
        else
           format.js  { render :action => "new" }
        end
      end
  end

I want to render the show.html.erb action not the show.js.erb action for an ajax request.

How can I do this?

UPDATE

This seems to work for me:

I put this in my application controller:

def redirect_to(options = {}, response_status = {})
      if request.xhr?
        render(:show) {|page| page.redirect_to(options)}
      else
        super(options, response_status)
      end
    end

Now I can redirect with an Ajax call.

 format.js {redirect_to @board}

Thanks to this forum:

Upvotes: 1

Views: 6692

Answers (2)

chell
chell

Reputation: 7866

This seems to work for me:

I put this in my application controller:

def redirect_to(options = {}, response_status = {})
      if request.xhr?
        render(:show) {|page| page.redirect_to(options)}
      else
        super(options, response_status)
      end
    end

Now I can redirect with an Ajax call.

 format.js {redirect_to @board}

Upvotes: 0

Dmitry Maksimov
Dmitry Maksimov

Reputation: 2861

Set dataType to "html" in your AJAX request and Rails will correctly handle AJAX request and render html, not js.

Upvotes: 1

Related Questions