observe_field in a rails 2.2.2 app is not saving checkbox value
I have the following form in a rails app which is not working:
<% form_remote_for :team do |f| %>
<tr>
<% 1.upto(12) do |i| %>
<td>
<%= f.check_box @mo[i] %>
<%= observe_field 'team_' + @mo[i],
:method => :put,
:with => "$(@mo[i] + '=开发者_如何学编程') + $('team_'+@mo[i]).value" %>
</td>
<% end %>
</tr>
<% end %>
@mo[i] is the name of the field in the database (e.g. jan, feb, mar,... dcm). The code would look like this if i were to write them all out:
<% form_remote_for :team do |f| %>
<tr>
<td>
<%= f.check_box :jan %>
<%= observe_field 'team_jan',
:method => :put,
:with => "'jan=' + $('team_jan').value"
</td>
...
</tr>
<% end %>
Thanks for your help.
Your :with statements has some syntax problems, and you are also not interpolating your ruby variables correctly. Try this:
:with => "'#{@mo[i]}=' + $('team_#{@mo[i]}').value"
I derived this by taking your intended output 'jan=' + $('team_jan').value
and replacing all instances of jan
with #{@mo[i]}
, which is the way to interpolate your variable that contains the month name.
Also, you probably need to add a :url
option to tell where your ajax call should be sent. Something like this:
<%= observe_field 'team_jan',
:url => team_path(@team),
:method => :put,
:with => "'#{@mo[i]}=' + $('team_#{@mo[i]}').value" %>
I am guessing here that your desired endpoint is team_path(@team)
, but replace it if it is not.
精彩评论