1

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:

chell
  • 7,646
  • 16
  • 74
  • 140
  • Does this question help? http://stackoverflow.com/questions/339130/how-do-i-render-a-partial-of-a-different-format-in-rails – spike Sep 05 '11 at 03:29
  • Thanks Spike. Not exactly what I was looking for. I want to render an html page instead of the js page for a AJAX request. – chell Sep 05 '11 at 04:58
  • The suggestions about giving the full filename with :file don't work? My test seemed to work (:file => "controller/show.html.erb") – spike Sep 05 '11 at 05:03
  • I am sure it works. I just thought what I was looking for was more like the solution I posted. I will try your suggestion as well. – chell Sep 05 '11 at 07:11

2 Answers2

1

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

Dmitry Maksimov
  • 2,861
  • 24
  • 19
  • Holy Cow I will try that. Thanks Dmitry – chell Sep 05 '11 at 11:12
  • Any idea how to set the dataType on a Rails form? – chell Sep 05 '11 at 11:18
  • Nice but in my case I want the form submitted with ajax. If there is an error then do some jquery stuff. If not then redirect_to the show page. Changing the dataType won't let me have both behaviors like the method I posted above. – chell Sep 05 '11 at 11:29
0

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}
chell
  • 7,646
  • 16
  • 74
  • 140