redirection in format.json ...

I have presently a render :json which modify the current page

      if @request.save          format.html { redirect_to(@request) }         format.json { render :json => { :result => 'success', :request => request_path(@request) } } ....

but I would like rather to redirect to an index page I wrote this but it doesn't redirect ...

        format.json { redirect_to requests_url }

what should I write to redirect correctly to an html page ?

thanks for your help

erwin

Here is an example:

format.json { render :json => { :redirect => requests_url } }

and then on your client redirect like this:

if (json.redirect) {

window.location = json.redirect; }

Also try http://www.google.com/search?q=json+redirect

Does this solve your problem?

/Lasse

Lasse Bunk wrote:

Here is an example:

  format.json { render :json => { :redirect => requests_url } }

and then on your client redirect like this:

  if (json.redirect) {     window.location = json.redirect;   }

Also try json redirect - Google Search

Does this solve your problem?

/Lasse

thanks for your help

it seems that json.redirect is bypassed... I put an alert, and it's not fired up

if (json.redirect) {    alert('redirect');   }

in my controller the create action is :

    respond_to do |format|       if @request.save         flash[:notice] = 'Request was successfully created.'         format.html { redirect_to requests_url }         format.xml { render :xml => @request, :status => :created, :location => @request }         format.json { render :json => {:result => 'success', :redirect => requests_url } }         format.js       else .....

once the request is saved, ... I get in the log

Completed in 563ms (View: 1, DB: 5) | 200 OK [http://localhost/requests\]

I should have 200 OK [http://localhost:3000/requests\]

Maybe you’re not requesting it properly, making sure that you call the format.json part? It seems it redirects to requests_url in format.html.

/Lasse

Just to clarify: A proper json request is to call /path.json or call /path (with or without .json) with an “Accept: application/json” header.

/Lasse

Lasse Bunk wrote:

Maybe you're not requesting it properly, making sure that you call the format.json part? It seems it redirects to requests_url in format.html.

/Lasse

yes... after some tests, I rewrote the original format.json line (from the plugin uploadify_rails) as :

        format.json { render :json => { :result => 'success', :request => member_request_url(@request[:id]) } }

this redirects to :show (don't misunderstand it , 'request' is my model instance...) and I just modified the :show action as following :

    respond_to do |format| ..       format.js {         render :update do |page|           page.redirect_to member_requests_url         end       }     end

this time, the redirection is fine.... which is enough for my test, I'll try to investigate a little bit more to understand why:

        format.json { render :json => { :result => 'success', :request => member_request_url(@request[:id]) } }

redirects to :show with a format .js

I thought I could write a :callback => 'index', no change... it's always redirected to :show... OR is it an issue in using :request => maybe this is a parameter used by the render.json ...

thanks a lot for your help

Again: Make sure that format.json is in fact called. Maybe try removing format.html to make sure that the redirect isn’t done there.

/Lasse