0

Controller has these routes: (simple example for illustration)

get :index do
  render "person/index" #this view has the .ajax action in it
end

put :update, :with => :id do
  @person = Person.find(params[:id])
  render "person/details"
end

The .ajax call from the index.slim file:

onTestButtonClicked: function() {
  return $.ajax({
    type: "PUT",
    url: "/update/" + contact_id,
    data: {
      key: value
    },
    success: function(data) {
      return console.log("success");
    }
  });
}

In the end what I'm trying to accomplish is to just have the render "person/details" line from the update route replace the whole view like it would if this were a GET or a submitted form. The data that is actually being passed successfully is a JSON object used to validate some information, but simply redirecting the window on success won't actually do what I need neither will replacing part of the DOM with some part of the result data in the success callback.

Am I missing something simple here to make this work?

Pelted
  • 81
  • 4
  • if you want to replace the whole page why are you using ajax? – José Barbosa Feb 05 '14 at 11:39
  • That's a good question and one I thought of this morning after fighting with this problem until rather late. The reason is I need to actually send a JSON object with PUT to the server. The reason for this is unfortunately not optional in this instance. I guess maybe I'm looking for a way to do a PUT or POST from JS that is NOT ajax. – Pelted Feb 05 '14 at 17:13
  • 1
    Anyway, I think you have access to **render "person/details"** in **success** of your ajax call. Something like: **$('body').html(data);** maybe works. – José Barbosa Feb 05 '14 at 17:17

1 Answers1

0

To expand on José Barbosa's comment: data in your success callback likely contains the full page's HTML. However you probably want to disable the layout:

render("person/details", layout: !request.xhr?)
dzuc
  • 761
  • 8
  • 12