Jason Varga
Jason Varga

Reputation: 1959

Rails remote: true only if requested through ajax

I've got my app working as I want it without AJAX.

I'm adding AJAX now to an item listing page with an 'add item' button.

Normally, clicking 'add item' it'd just go to the new item page, but with ajax it injects the new item form onto the page. On submit it updates the listing. This is all working fine, too.

However, if I visit the new item page directly, upon submitting the form it still fires the javascript. I want avoid any AJAX form submission if I'm on the form page itself.

Is it possible to only add remote => true to my form tag if the page has been requested through AJAX?

(...or suggest a better way to do this)

Upvotes: 1

Views: 282

Answers (1)

Shane Andrade
Shane Andrade

Reputation: 2675

request.xml_http_request? will tell you if you are serving an ajax request:

<% if request.xml_http_request? %>
....
<% end %>

Upvotes: 5

Related Questions