Hi,
I’ve got an ajax call who’s response does some complicated things on
the view. I’ve already created this view in ruby on the server, so I’d
rather not rewrite the entire thing in javascript when the ajax call
returns.
It seems like it should be easy enough to resend the current view
(minus the layout) in response to the json call, but I’m having
trouble figuring out what should go in the controller.
In other words, here’s the standard controller action:
POST /my_controller
POST /my_controller.json
def create
@my_controller = MyController.new(params[:my_controller])
respond_to do |format|
if @my_controller.save
format.html { redirect_to :back }
format.json { render json: @my_controller, status: :created,
location: @my_controller } # <— here
else
format.html { redirect_to :back }
format.json { render json: @my_controller.errors, status:
:unprocessable_entity }
end
end
end
At the place I’ve marked “<— here”, I’d like to basically return
what would happen on “redirect :back” except I don’t want the round
trip to the browser, nor the layout, and I’d like to return it as part
of the json object.
In other words, I’m looking for something like this:
format.json { render json: { html:
get_html(request.env[‘REQUEST_URI’]) }, status: :created, location:
@my_controller } # <— here
def get_html(uri)
TODO: How do I render this and return it as a string?
Obviously not the following, but that is the idea:
html = render :template => uri, :layout => false
return html
end
I hope my intent is clear…
Thanks!