Topic: form_for: How to change the submit button as soon as it's clicked?

Hey! I'm teaching myself Ruby, and have been stuck on this for a couple days:

* I'm currently using MooTools-1.3-compat and Rails 3.

* I'd like to replace one button (called "Follow") with another (called "Unfollow") as soon as someone clicks on it. I'm using :remote => true and have a file ending in .js.erb that's being called...I just need help figuring out what goes in this .js file

* The "Follow" button is in a div with id="follow_form", but there are many buttons on the page, and they all have an id = "follow_form". I only want to replace the button that made the call.

ps. This is what I have so far, and this works:

in app/views/shared:

<%= form_for => @event), :remote => true do |f| %>
  <div><%= f.hidden_field :event %></div>
  <div class="actions"><%= f.submit "Follow" %></div>
<% end %>

in app/views/events/create.js.erb

alert("follow!"); //Temporary...this is what I'm trying to replace

in app/controllers/subscriptions_controller.rb*

def create
  @subscription =[:subscription])
  respond_to do |format|
    format.html { redirect_to(..) }
    format.js {render :layout}

Any help would be greatly, greatly appreciated!

Last edited by sdc (2011-01-12 16:55:39)

Re: form_for: How to change the submit button as soon as it's clicked?

In your RJS file, page represents the DOM and you select by element id, so -
1) You need to provide your submit button with an ID.
2) In the rjs file, you can get your submit button element with page['ID_FOR_SUBMIT_BUTTON'], then take it a bit further to update the value:

page['ID_FOR_SUBMIT_BUTTON']['value'] = "Unfollow"

Not tested, but if that does not work, then you may have to do this in your rjs file:

page['ID_FOR_SUBMIT_BUTTON'].write_attribute('value', 'Unfollow')