0

I have a form with radio buttons on a page. I also have a helper method called vanish that adds
raw' style="display: none;" '.

In my view I would like to be able to call a boolean value based on which radio button is selected to activate/deactivate the helper method. The goal would look something like this

<p <%= vanish unless x %> > Stuff that only shows when a certain radio button is selected </p>

I am trying to do this without JavaScript. I am using Ruby on Rails 3.2.13

If you need anything else let me know.

Edit. I have seen this done in rails 2.3 where x would be params[:name] != 'value. This however does not dynamically change the page in rails 3.2

1 Answer 1

2

If you need this stuff to change after the page loads, you must use JavaScript. All your Ruby code is going to be executed before the user sees the page.

If this only has to do with the initial view of the page, then you're fine - all you need to do is have a variable that determines which button starts out selected, and use it for both the conditional (x, in your code), and elsewhere in the view where you display the radio buttons - see Rails - How to make a conditional Radio Button checked? for details on setting a default selected radio button like this.

Sign up to request clarification or add additional context in comments.

3 Comments

Interesting. Any idea if it was different in an earlier version of rails. In 2.3 that boolean would be a params[:*] == 'name', but that does not work in 3.2.
I'm not familiar with that usage, but JS has always been required to make pages respond when the user interacts with form elements. Older version of Rails sometimes tried to write JS for you, which may have been what you describe. They've moved away from that in the last several versions.
I will double check, when I looked at the older example I did not see any traces of JavaScript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.