All threads / How do I build conditional form fields?

Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do I build conditional form fields?

Satchit Shenoy asked in General

I'm trying to build a form which contains categories and subcategories. The desired effect is to display the subcategories field or the relevant subcategories depending on the category that has been selected. I found some random options on the net, but nothing that works in the desired manner. How do I go about this?

Thanks

For most of these, I've found that the easiest way (for me at least) is often to write some custom JS to handle it. Basically when the first thing is selected, use an AJAX request to load up some JSON for the desired subcategories and then use those to update the options available in the form. Something like this: https://kernelgarden.wordpress.com/2014/02/26/dynamic-select-boxes-in-rails-4/

I'll have do an episode on this soon, because this is a really common thing to do, but not well documented.

Hi Chris,

Tried that one - didn't particularly work for me. Looking forward to the episode!

Thanks!

Was there an episode made regarding this issue?  I looked and didn't see anything, so I just wanted to make sure I didn't miss it.

-Thanks

Hi Chris :

Does this issue was treated ? I have seen information on nested forms and wizzard forms, but nothing on that specific point. I would be great to hace some well documented reference.

All the best,
Charles

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 34,674+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.