redirection in format.json ...

482 views
Skip to first unread message

Erwin

unread,
Mar 23, 2010, 2:35:55 PM3/23/10
to Ruby on Rails: Talk
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

Lasse Bunk

unread,
Mar 23, 2010, 5:06:17 PM3/23/10
to rubyonra...@googlegroups.com
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

2010/3/23 Erwin <yves_...@mac.com>

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.


Kad Kerforn

unread,
Mar 23, 2010, 5:40:25 PM3/23/10
to rubyonra...@googlegroups.com
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 http://www.google.com/search?q=json+redirect
>
> Does this solve your problem?
>
> /Lasse
>
> 2010/3/23 Erwin <yves_...@mac.com>

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');
}
--
Posted via http://www.ruby-forum.com/.

Kad Kerforn

unread,
Mar 23, 2010, 6:11:52 PM3/23/10
to rubyonra...@googlegroups.com
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]

Lasse Bunk

unread,
Mar 24, 2010, 4:41:49 PM3/24/10
to rubyonra...@googlegroups.com
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

2010/3/23 Kad Kerforn <li...@ruby-forum.com>

Lasse Bunk

unread,
Mar 24, 2010, 4:55:05 PM3/24/10
to rubyonra...@googlegroups.com
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

2010/3/24 Lasse Bunk <lass...@gmail.com>

Kad Kerforn

unread,
Mar 25, 2010, 2:52:11 AM3/25/10
to rubyonra...@googlegroups.com
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
>
> 2010/3/23 Kad Kerforn <li...@ruby-forum.com>

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

Lasse Bunk

unread,
Mar 25, 2010, 2:13:22 PM3/25/10
to rubyonra...@googlegroups.com
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

2010/3/25 Kad Kerforn <li...@ruby-forum.com>
Reply all
Reply to author
Forward
0 new messages