Rendering json versus js in rails - ruby-on-rails

I'm getting different results if I render js versus render json .Trying to figure out why this doesn't work quite as expected
My javascript looks like this:
$( function () {
$('.delete_post').bind('ajax:success', function () {
$(this).closest('tr').fadeOut();
}
)
});
Controller:
Works
respond_to do |format|
format.js { render :nothing => true}
end
Works
respond_to do |format|
format.js { head :ok }
end
Works
respond_to do |format|
format.json { render :json => {} }
end
Doesn't work
respond_to do |format|
format.json { head :ok }
end
Doesn't work
respond_to do |format|
format.json { render :nothing => true }
end
When I examine what is happening in the ajax response I see that the cases where it doesn't work I get a "parseError".
I understand that the Content-type getting sent back is different (text/javascript vs applicaiton/json), but I would still expect the last 2 cases to work since they are sending back a 200 to the server.
(Code derived from: http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3/)

format.json { head :ok }
and
format.json { render :nothing => true }
don't work because they're not returning valid json. head :ok will simply set the response header to 200 and :nothing => true will render a blank page/document which not valid json.

Related

Rails Template Missing

I recently updated a controller from a plain
render :json => #something
to
respond_to do |format|
format.html {
redirect_to #something
}
format.json {
puts "found json format"
format.json { render json: #something, status: :created }
}
end
But, now I'm getting ActionView::MissingTemplate errors. My question is, if I use the respond_to do |format| block, am I required to use a template? What if I just want to return plain json?
Btw, the puts statement in the json respond_to block is being called successfully.
I think the problem is puts being called inside of your format.json block and the nesting of format.json inside of it's own block. Try removing both (as below).
respond_to do |format|
format.html {
redirect_to #something
}
format.json {
puts "found json format"
format.json { render json: #something, status: :created }
}
end
Try:
respond_to do |format|
format.html { redirect_to #something }
format.json { render json: #something, status: :created }
end
You are telling that you accept HTML and JSON formats. Since your request comes from HTML you'll have the HTML template rendered.
If you want to return only JSON remove that line from your code. Also check the docs on respond_to.
Probably you already know it, but there's a guide on Layouts and Rendering.
Remove the format.json part from within the json rendering block (you have an unnecessary block within your block):
respond_to do |format|
format.html {
redirect_to #something
}
format.json {
render json: #something, status: :created
}
end
I think the reason your code is prompting an error it's because you have
format.json {
format.json { render json: #something , status: :created }
}
You should have:
format.json {
render json: #something , status: :created
}

What is the preferred way to return an empty JSON response in Rails 3?

When a user POSTs JSON to the /update/ action in a Rails 3 app, what is the best way to respond?
I want to just send an empty JSON response with a 200 code, something like
head :no_content
or
render :nothing => true, :status => 204
(examples from How to return HTTP 204 in a Rails controller).
Typically I have been doing this:
render :json => {}
or
render :json => 'ok'
Is there preferred or more Rails-y way to this?
My Rails 3 app uses code such as this for updates. The code for html and xml was auto generated by Rails, so I just added in the JSON renderer using the same format.
respond_to do |format|
if #product.update_attributes(params[:product])
format.html { redirect_to(#product, :notice => 'Product was successfully updated.') }
format.xml { head :ok }
format.json { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #product.errors, :status => :unprocessable_entity }
format.json { render :json => #product.errors, :status => :unprocessable_entity }
end
end
Works perfectly, which is what's ultimately important.

Why is my rails controller not working?

I am trying to run a javascript line from my controller as a response to an AJAX call :
respond_to do |format|
if params[:return_to] == 'monthly_calendar'
render js: "alert('hey Trip!');"
else
format.js { render :partial => #task, layout: false }
format.html { redirect_to things_url }
format.json { render :json => #object.to_json }
format.xml do
if #task.new_record?
render :xml => #task.errors.to_xml
else
render :xml => #task.to_xml, :status => '201 Created'
end
end
end
Instead this returns a 406. Any ideas?
It most certainly goes to the correct call in this statement. But doesn't know what to do with the javascript call, and returns a 406.
Change it to this
respond_to do |format|
if params[:return_to] == 'monthly_calendar'
format.js { render js: "alert('hey Trip!');" }
else
.....
Because you are not specifying to what format are you responding, e.g. format.html { #your code here } when params[:return_to] == 'monthly_calendar'. Check documentation of respond_to for more information.
You can render a full view task.js.erb instead of a partial.
Here is a good tutorial on what you are trying to accomplish:
http://blog.bernatfarrero.com/jquery-and-rails-3-mini-tutorial/
I hope it helps.
Good luck.
Try respond to the js format:
format.js { render js: "alert('hey Trip!');" }

How to manipulate response status code from a Rails Controller action

Given a classic controller action. Have a look at the MARK. I need to set the status code to 200 for the response. Background: swfupload an ajax file upload solution seems to send data in a wrong format.
I tried response.headers['Status'] = 200, response.status 200, render :json => 'data', :status => 200. But the response's status code doesn't change.
def create
if params[:Filedata]
#medium = Medium.new(:swf_uploaded_data => params[:Filedata])
else
#medium = Medium.new(params[:medium])
end
respond_to do |format|
if #medium.save
format.html { redirect_to(#medium, :notice => 'Medium was successfully created.'); }
format.xml { render :xml => #medium, :status => :created, :location => #medium; }
MARK
else
format.html { render :action => "new" }
format.xml { render :xml => #medium.errors, :status => :unprocessable_entity }
end
end
end
The status code can only be declared within the return commands like render, redirect_to .... and will affect to this return command, there is no way to set the code for all the responses
For format.html { redirect_to(#medium, :notice => 'Medium was successfully created.'); } since is a redirection the code will be a 3XX and you can't change it or the redirection won't work
For format.xml { render :xml => #medium, :status => :created, :location => #medium; } you are declaring the status as created, this means for rails that the code is 201, for making it a 200 change it for:
format.xml { render :xml => #medium, :status => :ok, :location => #medium; }
What format is the AJAX call expecting in response? You're only responding to HTML and XML, so if it's expecting JavaScript or JSON, that might be a problem.
Also, I'm not sure if this matters, but I've never seen a condition inside the respond_to block like you've shown. Usually it's like this:
if #medium.save
respond_to { |format| ... }
else
respond_to { |format| ... }
end
I'm also not clear what the "MARK" is supposed to be.

Ruby on Rails' respond_to causing strange error

There is another respond_to for the usual case, and a special case when a param[:top] is passed in, so there is another respond_to earlier in the code:
respond_to do |format|
format.html { render :top_page_analytics }
format.json { render :json => #analytics }
format.xml { render :xml => #analytics }
return
end
but the above code actually gave a strange error for missing template for json, and further debug leading to:
respond_to do |format|
format.html { render :top_page_analytics }
format.json { render :json => #analytics }
format.xml { render :xml => #analytics }
end
return
which fixes the bug. The return is needed so that there will be no "double render error" because the program will flow to the other respond_to. But I wonder the strange syntax of respond_to, looking somewhat like a case statement, may cause error like that at the top?
The return can't go there because you're passing a block. The block isn't executed in the immediate context of the controller action. When you return from the block, you're actually returning from the function yielding (respond_to), not the controller action.

Resources