Is there such a thing as button_to_remote?

I've changed my destructive link_to statements in favor of button_to.

But what if I want to make an .rjs call with that button?

Seems like we need something equivalent to link_to_remote, so that the
button can make an ajax request instead submitting the form.

Or am I missing the point entirely?

Jeff

I've changed my destructive link_to statements in favor of button_to.

But what if I want to make an .rjs call with that button?

Seems like we need something equivalent to link_to_remote, so that the
button can make an ajax request instead submitting the form.

button_to takes a javascript function... so pass in an ajax function to call using remote_function....

http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M000534

-philip

Hi Jeff,

Jeff wrote:

I've changed my destructive link_to statements in
favor of button_to.

But what if I want to make an .rjs call with that button?

Seems like we need something equivalent to link_to_remote,
so that the button can make an ajax request instead submitting
the form.

I've made pretty extensive use of form_remote_tag for this. I just don't put any user input elements inside, although I do sometimes put hidden fields inside to pass param values. Just be aware that you can't use it inside another form like you can with link_to_remote.

hth,
Bill

Hi Philip,

Philip Hallstrom wrote:

button_to takes a javascript function... so pass in
an ajax function to call using remote_function....

Could you say more about this, please? What do you mean "button_to takes a javascript function"? I wasn't able to find anything about that when I went though the exercise. I still don't see it but would really like to know how it could be made to work. A snippet would be excellent!

Thanks,
Bill

I've been working on much the same problem, but I have a form that
when I submit it I want to only reload part of the page. I tried
working it like I have other ajax/RJS calls, but it doesn't seem to
work from a submit_tag. I don't know the name of the javascript
function that the rjs creates so I don't know how to call it.

Maybe you can help me with a related question, then... when I try a
simple remote form like this:

<% form_remote_tag(:url => products_path, :html => { :id =>
'product_form' }) do %>
  <h2>Add Product</h2>
  <label for="product_title">Title:</label>
  <%= text_field 'product', 'title' %>
  <%= submit_tag "Add New Product" %>
<% end %>

This will call my create action just fine as expected; but
unfortunately it will respond_to html, not js, and hence render the
wrong template! Is this the correct behavior? I don't think it is.
Did I do something wrong in the above code?

Thanks again,
Jeff

Hi,

def button_to_remote(name, options = {}, html_options = {})
  button_to_function(name, remote_function(options), html_options)
end

Cheers, John

Hi Jeff,

Jeff wrote:

when I try a simple remote form like this:

<% form_remote_tag(:url => products_path,
:html => { :id => 'product_form' }) do %>
<h2>Add Product</h2>
<label for="product_title">Title:</label>
<%= text_field 'product', 'title' %>
<%= submit_tag "Add New Product" %>
<% end %>

This will call my create action just fine as expected; but
unfortunately it will respond_to html, not js, and hence
render the wrong template! Is this the correct behavior?

Sorry, but I'm not sure I'm following you. I'm guessing you're saying
you're using respond_to in your create action and that it's rendering an
RHTML template instead of the RJS template you expect. Is that right? If
so, the problem is probably in how you've coded the respond_to. I took me
some time to get my arms around that. A little sandbox time goes a long
way. Create a new sandbox, cut the code down to the bare minimum needed to
get the behavior that you're testing. If you're still in need, I'm happy to
take a look at the controller and view code at that point if you want to
post it.

Best regards,
Bill

I don't think it is.

> This will call my create action just fine as expected; but
> unfortunately it will respond_to html, not js, and hence
> render the wrong template! Is this the correct behavior?

Also make sure that you've included the required javascript in your
layout. Something like:

  <%= javascript_include_tag :defaults %>

If I remember correctly I was having a similar problem to yours and
the reason was that I wasn't including javascript.

Jesse

Yes, that was it! Unforunately I didn't even see your reply until the
day after I figured it out (stupid Google Groups strikes again).

Thanks so much.
Jeff

isn't the whole point of button_to to make sure you support non-JS
users? [well, mebbe not the whole point.] :wink: you could probably just
add custom js to a regular button_to and have both rjs fun and not
leave non-JS users out in the cold.

RSL