aris
June 28, 2012, 1:45am
1
Hi,
I’m trying to use the form_for reset button:
<%= button_tag ‘Reset’, :type => ‘reset’ %>
It works great, but when I update the object through AJAX, how can I get
the new form values to be the ones reset to? Right now it resets to the
values when the document loads, which makes sense. But is there a way to
get the reset button to register the new values?
https://github.com/rails/jquery-ujs/issues/265#issuecomment-6616407
Here’s the code:
_edit_row.html.erb
<tr id='event_edit_<%= event.id %>' style='display:none;'>
<%= form_for(event, {:remote => true, :namespace => :blah, :html => {:class => ['blah', 'event']}}) do |f| %>
<td class='edit title'><%= f.text_field :title %></td>
<td class='edit description'><%= f.text_area :description %></td>
<td class='edit submit'><%= f.submit %></td>
<td class='edit cancel'><%= button_tag 'Reset', :type => 'reset' %></td>
<% end %>
</tr>
_view_row.html.erb
<tr id='event_view_<%= event.id %>'>
<td class='view title'><%= event.title %></td>
<td class='view description'><%= event.description %></td>
<td class='view modify'><%= link_to 'Edit', edit_event_path(event) %></td>
<td class='view delete'><%= link_to 'Destroy', event, :method => :delete, :data => { :confirm => 'Are you sure?' } %></td>
</tr>
events.js
$(document).ready(function() {
$('td.edit.cancel button').event_form_reset();
});
$.fn.event_form_reset = function () {
this.click(function(event) {
var confirmed = confirm('Are you sure you want to discard your changes?');
if (confirmed == true) {
$(this).parents('tr').toggle();
$(this).parents('tr').prev().toggle();
This file has been truncated. show original
There are more than three files. show original
Thank you for your time!
A_L
June 28, 2012, 1:50am
2
And hopefully something simpler than looping through all the fields and
manually setting their default values: