Rails - I need to check the box to change the field in the database

I know that I did this before, but for life I can not understand.

I have a table with a β€œcalled” field in it. I need to use a checkbox to update db and check if "called" is "true" or not. No need to be AJAX, just need to update the field.

table: rsvp field: called

Thanks a ton.

+4
source share
3 answers

A simple approach without ajax can use the checkbox inside the form and submit the form with the javascript onclick event flag.

Example:


View

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

this if you are using jQuery ... with the prototype of the onclick line would be:

 $('rsvp').submit() 

controller

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

Link:

check the box

+5
source

In sight:

 <% 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]) 
+2
source

If you just want to do this simply by clicking on the checkbox, you need to follow the Ajax road. Try using "view_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 are here .

Remember to set up your routes so that you can find the mark_as_called action.

+2
source

All Articles