Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - I need a checkbox to change a field in the DB

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.

like image 743
Eric Goodenough Avatar asked Jan 23 '23 20:01

Eric Goodenough


1 Answers

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

like image 84
makevoid Avatar answered Jan 25 '23 09:01

makevoid