I'm trying to get some json from an action but I have a problem.
I know that you can return "format.js" and return an ".js.erb" file but... Imagine I have an action that return some stuff and I want to work with that in two different ways. I cannot have that two different ways on the same .js.erb because when I return it, both ways will be executed, right? I mean, when I return that file, that file will be processed entirely.
So I tried to use the jQuery way.
I have:
# GET /posts/slug/hidden_content.json
def hidden_content
@post = Post.find(params[:id])
@post = hidden_post_part(@post.body)
respond_to do |format|
format.json { render json: @post, :status => :ok}
end
end
And in the controller.coffee:
$ ->
# Render hidden part of post
$(".showallpost img").click( ->
post_url = $(this).next().attr("href").split("/").pop();
$.ajax
url: Routes.hidden_content_path(post_url),
type: "GET",
contentType: "application/json",
success: (data) ->
console.log "foo"
);
The GET works perfectly and it returns what I need but the success function is never called, I don't see the "foo" log.
I guess that the action should return something to tell to the caller that the GET was successful. (I tried with :status without luck).
So I can't work with my GET data.
Ideas?