sam
sam

Reputation: 251

Ruby on Rails show field when checkbox checked WITHOUT JAVASCRIPT

I feel like I'm going insane. This seems so simple but somehow I cannot find an answer on the internet.

I have a form with a checkbox in it. If the checkbox is checked, I want to display an input field. How do I do that in Ruby on Rails without Javascript? All the solutions I saw were with Javascript for like 9 years old.

= simple_form_for(@register) do |f|

  .form-inputs
    = f.input :first_name
    = f.input :last_name, required: true
    = f.check_box :special_request
    = f.label :special_request

# and now here should be the if-else statement that should go something like this
   if :special_request.checked?
    = f.input :request

But I have yet to find out how exactly this is supposed to be implemented. It seems like the easiest task, but I have yet to find an answer. Also, no matter if the checkbox is checked or not, the value is always 1 when I look at it in with inspect.

Upvotes: 1

Views: 904

Answers (1)

eikes
eikes

Reputation: 5061

The closest you'll get to this is the "Checkbox Hack"

.control-me {
  display: none;
}

#toggle:checked ~ .control-me {
  display: block;
}
<input type="checkbox" id="toggle">
<div class="control-me">
  <input type="text">
</div>

Adapted from:

https://css-tricks.com/the-checkbox-hack/

Upvotes: 1

Related Questions