Monday, February 04, 2008

form_remote_tag with ajax and none ajax action

Does anyone else having the same problem as I do when trying to set the url for AJAX and none AJAX actions for the form_remote_tag? I always seem to forget how to do this. It always take me a couple of tries to get this right. Anyway, I am putting the correct way to do this here so I come back later when I forget.

<% form_remote_tag :url => {:controller => '/posts', :action => 'view'},
:html => {:action => {:controller => '/posts', :action => 'view', :id => @id}} do %>
<%= submit_tag 'View' -%>
<% end %>

<form action="/posts/view/1" method="post"
onsubmit="new Ajax.Request('/posts/view/1',
{asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;">
<input type="submit" value="View" />
</form>

8 comments:

Anonymous said...

What is the significance of the dashes at the end of a ruby string in your rhtml files?

i.e.:
%= submit_tag "View" -%

Also, it's next to impossible to get a form that doesn't include some form of deprecation.

I'm learning Rails to by the way, I wish there was a better place out there to get all the answers to those n00b questions. Sure there are some wikis but they are usually too confusing, or they hold your hand too much. Would you want to make a Ruby for n00bs wiki with me? We'd just kind of aggregate all the data that we have found in the far reaches of the Ruby universe. It would not only give back to the community but I expect it would help us both, in furthering our knowledge of Ruby on Rails. If you're up to it let me know, my email is smackywentz at yahoo dot com.

Anonymous said...

i so get what you mean! - i will bookmark this page right away :)

richard said...

Thanks for the post, Conrad.

Question: what if you want to include an input in the post params?

When I add a "text_field_tag :name" (for example) inside the form just above the submit tag, it ignores the text field, doesn't show up in the params at all.

Thanks!

Conrad said...

Richard,

It should work with the text_field_tag. I tried it out and it works on my machine. I am using rails 2.1.2.

Here is how my form tag looks like.

<% form_remote_tag :url => {:controller => '/post', :action => 'view'},
:html => {:action => {:controller => '/post', :action => 'view', :id => @id}} do %>
<%= text_field_tag :first_name %>
<%= submit_tag 'View' -%>
<% end %>

Here is the log of the parameters being send to the server.

Parameters: {"commit"=>"View", "authenticity_token"=>"02804752e2a1278fc2ebd92c2b9384c83a8aeb8f", "action"=>"view", "controller"=>"post", "first_name"=>"Conrad"}

Tarun said...

thanks a lot man... was struggling with it for the past 15 minutes..

Anonymous said...

I sure would like to see you give it an ID and Name for javascript reference...

I suspect its impossible.

giemmatrolai said...

hey im facing the same problem, form_remote_tag doesn't pass parameters, as you can see details here: http://pastie.org/695136

cialis said...

Interesting article, added his blog to Favorites