4

I know I have done this before, but for the life of me I can't figure it out.

I have a table with a "called" field in it. I need to use a checkbox to update the db and check if "called" is "true" or not. Doesn't need to be AJAX, just needs to update the field.

table: rsvp field: called

Thanks a ton.

3 Answers 3

5

A simple approach without ajax could be using a checkbox inside a form and submitting the form with the checkbox javascript onclick event.

Example:


View:

<% form_for @rsvp, :id => "rsvp" do |f| %>
  <%= f.check_box :called, :onclick => "$('#rsvp').submit()" %>
<% end %>

this if you are using JQuery... with prototype the onclick string will be:

$('rsvp').submit()

Controller:

@rsvp = Rsvp.find(params[:id])
if @rsvp.update_attributes(params[:rsvp])
  # success
else 
  # fail
end

Reference:

check box

Sign up to request clarification or add additional context in comments.

Comments

2

In view:

<% form_for :rsvp, :url => {:controller => "rsvp", :action => "update"} do |f| %>
   <%= f.check_box :called %>
   <%= f.submit "Update" %>
<% end %>

In rsvp controller, update method:

@RSVPobject.updateAttribute(:called, params[:rsvp][:called])

1 Comment

Thanks. I am looking to update the field just by clicking the checkbox, not submitting a form. Any other ideas? Cheers!
1

If you just want to do this just by clicking the checkbox, you need to go the Ajax road. Try using an "observe_field" in your view.

<%= observe_field ":called",
                    :frequency  => 0.25,
                    :update     => 'feedback_to_user',
                    :url        => {:action => :mark_as_called},
                    :with       => 'called',
                    :on         => 'click' %>

All the details here.

Don't forget to adjust your routes so that the "mark_as_called" action can be found.

1 Comment

I wouldn't specify the frequency since that means it would use polling. Instead omit that option or specify 0, and the code will use the event observer instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.