Remote form_tag in rails3 without a named route

What is the proper spell to do this essentially asynchronously?

form_tag :controller => :magic, :action => :search, :method => post, :remote => true do 

method = post and remote = true just sound at the end of the url instead of actually making it an ajax message.

+7
ruby ruby-on-rails ruby-on-rails-3
source share
3 answers

The only way I found this is to wrap the url parameters in the url_for method.

 form_tag url_for(:action => :create, :id => @artist.id), :remote => true do 

However, if you need to pass a method parameter, you may need to wrap it and deleted it in parentheses.

+15
source share

Here is what you need:

 form_tag( { :controller => :magic, :action => :search, :method => post }, { :remote => true } ) do .... 

This is kind of poor design, but the Rails form_tag methods require two hashes - a hash of url_for parameters and regular hash settings. This caused a lot of confusion for many Rails programmers. If you do not add hash boundaries, all parameters are passed to url_for (), destroying :remote => true .

This will add data-remote="true" to your form (for use with unobtrusive javascript, as others have mentioned). From there, you need to create the appropriate AJAX for the binding to complete the request.

Although you really should use a named route like magic_search_path , instead of controller / action parameters.

+9
source share

I believe that form_remote_tag should be used instead:

 form_remote_tag(:url => { :controller => :magic, :action => :search }) do 

The default method is http.

If you want to pass additional parameters to form_tag , you need to make them into a separate hash from the URL parameters, for example:

 form_tag { :controller => :magic, :action => :search }, { :method => post, :remote => true } do 
0
source share

All Articles