Los rieles evitan el diseño durante la solicitud de AJAX

I have searched around and have not been able to find a solution for this type of mechanic. I want to load all pages normally in Rails, but whenever I do an ajax request I just want to return the page without the layout. So anytime I make an ajax requst I can append a ?page=true or something along those lines and have Rails just return the page without the layout.

Is this possible? Is there a better way to do it that I am missing?

Gracias por cualquier ayuda!

Final Solution Working Code:

In the controller all you need to do is append a little logic to the format.html en la respond_to bloquear.

En la show método por ejemplo

def show
    # code beforehand

    respond_to do |format|
        format.html { render :layout => !request.xhr? }
        # other formats

And that's it! Prevent layouts during AJAX requests!

Nota: Thanks to the smathy's comment on his answer this was simplified further. I originally had format.html { render :layout => nil if request.xhr? } This solution works just as well, but smathy's modification keeps it even simpler.

preguntado el 02 de julio de 12 a las 02:07

Is it possible to do the same in the application controller for every ajax request? -

1 Respuestas

You don't need to add that parameter, request.xhr? will return true in your controller when it's an Ajax request. Just use that to decide whether to render the layout or not.

Respondido 02 Jul 12, 02:07

How would that work? I tried if request.xhr? render :layout => nil end in the show controller. It is returning an error. Either I am not using request.xhr? correctly or I am trying to remove the layout incorrectly. - KayoticSully

I was able to figure it out, THANK you for the info. I will edit the question with my actual final working code. Thanks to your information, my solution is very "Rails Like" and I am extremely happy with it! - KayoticSully

Nice solution, yes. An alternative is to use the boolean itself - but negated. Eg. I would do: render :layout => !request.xhr? - inteligente

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.