boddhisattva
boddhisattva

Reputation: 7390

Set checkbox default to true on rails

I'm trying to set the default value of a check box to be true by default using check_box helper in Rails. I couldn't find the solutions in the Rails api documentation

This seems to be real easy with check_box_tag, but I need to use the above helper as I require it for a model object.

Is it possible to use check_box_tag for a model object..? Doesn't look to be so from the documentation.

Could you please suggest any javascript, jquery work arounds if I can't directly implement this in Rails.

I'm on Rails 2.0.2 for project specific purposes.

Any guidance on this would be really handy..

Thank you very much..

Upvotes: 0

Views: 1159

Answers (2)

boddhisattva
boddhisattva

Reputation: 7390

I made use of check_box_tag, as I was able to find an efficient workaround to suit my requirement with help..

This is how one could use the same:-

<%=check_box_tag "model[local_variable]",1,true %>

This probably shows how more versatile is check_box_tag ..

Upvotes: 0

Dylan Markow
Dylan Markow

Reputation: 124439

Since the checkbox is tied to one of your model's fields, all you need to do is set the value to true on your model:

In your controller:

def new
  @model = Model.new
  @model.my_value = true
end

In your view:

<%= f.check_box :my_value %>

Upvotes: 4

Related Questions