# Rails 3 e Ajax

**URL:** <https://www.ruby-forum.com/t/rails-3-e-ajax/209923>\
**Category:** IT\
**Created:** [July 26, 2011, 3:16pm UTC](https://www.ruby-forum.com/t/rails-3-e-ajax/209923 "2011-07-26T15:16:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [July 26, 2011, 3:16pm UTC](https://www.ruby-forum.com/t/rails-3-e-ajax/209923/1 "2011-07-26T15:16:14Z")

</div>

Stavo creando un’applicazione di prova con la rc4 di rails 3.1 e  
verificare il funzionamento di un pò di cose tra cui jquery e la “nuova”  
gestione ajax di rails 3, in precedenza usavo rails 2 con prototype e  
lowpro. Tutto molto bello ma non riesco a cambiare al volo la modalità  
di invio della richiesta ad esempio cambiare un form dalla/alla modalità  
asincrona.

Ho il seguente form:

\<% remote = true %\>  
\<%= form\_for :article, :remote =\> remote, :html =\> { :method =\> :get,  
:id =\> “form\_search” } do |f| %\>  
…  
\<% end %\>

 \<%= render 'list', :remote =\> remote %\>

In rails.js ho individuato il selettore:

$.rails = rails = {  
…  
// Form elements bound by jquery-ujs  
formSubmitSelector: ‘form’,

l’ho modificato nel mio application.js

$.rails.formSubmitSelector = ‘form:not([data-no-remote])’;

ma non viene rilevata la modifica o meglio, non viene aggiornato il bind  
(anche se è un .live()) ne aggiungendo l’attributo da console  
ne da codice:

$(’#form\_search’).live(‘ajax:success’, function() {  
$(this).attr(‘data-no-remote’, ‘true’);  
});

eppure viene utilizzato il metodo live che dovrebbe aggiornare i  
riferimenti:  
$(rails.formSubmitSelector).live(‘submit.rails’, function(e) {

seguendo col debug di firebug, esegue quella funzione nonostante il  
selettore dovrebbe escluderlo… ma c’è un’altra cosa inquietante:

> > > $(’#form\_search’).data(‘remote’);  
> > > true
> 
> > > $(’#form\_search’).attr(‘data-remote’);  
> > > “true”

imposto il valore false tramite il metodo data:

> > > $(’#form\_search’).data(‘remote’, false);  
> > > [form#form\_search.form\_ajax1 /articles]
> 
> > > $(’#form\_search’).data(‘remote’);  
> > > false

il metodo attr non rileva il cambiamento e neanche l’html non viene  
aggiornato!!

> > > $(’#form\_search’).attr(‘data-remote’);  
> > > “true”

neanche viceversa:

> > > $(’#form\_search’).data(‘remote’);  
> > > true
> 
> > > $(’#form\_search’).attr(‘data-remote’);  
> > > “true”
> 
> > > $(’#form\_search’).attr(‘data-remote’, ‘false’);  
> > > [form#form\_search.form\_ajax1 /articles]
> 
> > > $(’#form\_search’).data(‘remote’);  
> > > true
> 
> > > $(’#form\_search’).attr(‘data-remote’);  
> > > “false”

Sembra che qualcosa non funzioni come dovrebbe o sbaglio?
