ruby_noobie
ruby_noobie

Reputation: 205

How to create an object when a check_box is checked?

Is there any way to get a check_box to function in the same way as button_to, for example, so that when the box is checked an object is created?

The code for my button_to is as follows:

<%= button_to("Yes", :action => "create", :controller => "objects", :object => {:boolean => 'true', :object_1.id => current.id}) %>

I want this same event to occur, but when a check_box is checked rather than when a button is clicked. It would be cool if the object could be destroyed when the box is unchecked as well. Is this possible?

Upvotes: 2

Views: 107

Answers (1)

Matt Stevens
Matt Stevens

Reputation: 1124

In short, no unless you expand your interest into Javascript and AJAX. The long answer is that a checkbox returns no value at all if it is not checked, not a nil value, nothing. The returned parameter looks like this {} ie empty. Whereas you are much safer reacting to a value like {destroy: "yes"} which you can implement with a button.

If you are dead set on a check-box, maybe look at ways of using two checkbox images as your button (one for set, one for not set). Javascript is your friend here, but it is more work and I wouldn't personally do it.

Upvotes: 2

Related Questions