简体   繁体   中英

remote form_tag in rails3 without a named route

what is the proper incantation to make this actually post asynchronously?

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

method=post and remote=true just get squashed on the end of the url instead of actually making it an ajax post.

The only way I found to do it 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 the pass the method parameter you might need to wrap that and the remote in parentheses.

Here's what you need:

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

Its sort of a bad design, but the Rails form_tag methods require TWO hashes - the url_for options hash, and the regular options hash. This has generated much confusion for many Rails programmers. If you don't add the delineating hash boundaries, all of the options get passed to url_for(), ruining the :remote => true .

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

Though really you should be using a named route like magic_search_path instead of the controller/action parameters.

I believe you need to use form_remote_tag instead:

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

The default http method is post.

If you do want to pass extra parameters on a form_tag you need to make them into a separate hash from the url parameters, like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM