Rails 3: Unable to process form as javascript instead of HTML

I am trying to process a form from a view in a controller, but the data type is always sent as HTML instead of JS. The form is displayed in the following .js.erb file in the called variable:

Views / Ofertas / buscar.js.erb:

$('#variable').html("<%= escape_javascript( render('buscar')) %>"); 

which displays the following partial: views / ofertas / _buscar.html.erb:

 <%= form_for @search, :html => {:remote => true, :'data-type' => 'script', :id => 'search_form'}, :url => {:controller => 'ofertas', :action => 'buscar'} do |f| %> <%= f.text_field :search_words %> <%= f.submit 'Buscar' %> <% end %> <div id='tabla'> <%= render(:inline => @tabla) %> </div> 

Now I have tried every combination of data types in the form. 'script', 'js', deleting this parameter, etc .... And here is the controller: controllers / ofertas _controller.rb:

 def buscar ... respond_to do |format| format.js end end 

The strange thing is that the first time this method is called in the controller, from a link that looks like this in a rendered view:

 <a href="/ofertas/buscar?search_words=default" class="tabs" data-remote="true" data-type="js">lista de ofertas</a> 

This works fine, and the "variable" is populated with the form. The rail console is as follows:

 Started GET "/ofertas/buscar?search_words=default" for 127.0.0.1 at 2011-05-11 20:15:45 -0400 Processing by OfertasController#buscar as JS Parameters: {"search_words"=>"default"} ... SQL stuff happens here ... Rendered inline template (1.4ms) Rendered ofertas/_buscar.html.erb (2.9ms) Rendered ofertas/buscar.js.erb (4.5ms) Completed 200 OK in 69ms (Views: 43.7ms | ActiveRecord: 0.6ms) 

But when I use a visualized form to access the same controller method, the request is always processed as HTML. Below is the resulting HTML of the rendered view (after the GET on top):

 <form accept-charset="UTF-8" action="/ofertas/buscar" data-remote="true" data-type="script" id="search_form" method="post"> <div style="margin:0;padding:0;display:inline"> <input name="utf8" type="hidden" value="โœ“"> <input name="authenticity_token" type="hidden" value="Tpv4CESApK+Cf2tMchewm/B2nprBgEQKYmp7MvWBcfc="> </div> <input id="_search_words" name="[search_words]" size="30" type="text"> <input id="_submit" name="commit" type="submit" value="Buscar"> </form> 

When I submit a search form with the word "1234", the rail console looks like this:

 Started POST "/ofertas/buscar" for 127.0.0.1 at 2011-05-11 21:14:30 -0400 Processing by OfertasController#buscar as HTML Parameters: {"utf8"=>"โœ“", "authenticity_token"=>"Tpv4CESApK+Cf2tMchewm/B2nprBgEQKYmp7MvWBcfc=", "search_words"=>"1234", "commit"=>"Buscar"} ... SQL stuff happens here ... Completed 406 Not Acceptable in 25ms 

I understand that this is due to HTML processing (headers Accept application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 ) but I donโ€™t understand why the request is sent as HTML. Accept headers before submitting the form are as follows: */*, */*;q=0.5, text/javascript, application/javascript

Please help me, I was stuck on this all day and could not make progress. Thanks.

EDIT:

Well, I finally found a function that attaches the handleRemote () event to the submit form. It actually searches for all forms on the page, but since there is only one, this is good enough. Here is the code:

 $(function(){ $("#tag_lista") .live('ajax:complete', function(){ $("form[data-remote]").live('submit', function(e){ $.rails.handleRemote($("form[data-remote]")); e.preventDefault(); }); }); }); 
+4
source share
1 answer

I missed the point, but how do you handle the form feed?

If you attach an event to your page in order to process the presentation of the first form, you will have to re-attach the event to the newly created form, that is, after you submitted it for the first time.

Take a look at jQuery live so you can handle this.

+3
source

All Articles