All threads / Associating Radio Buttons With Specific Field Subsets In Rails
Ask A Question


You’re not receiving notifications from this thread.

Associating Radio Buttons With Specific Field Subsets In Rails

Chris Zempel asked in Rails

Situation: A user enters the name of a venue they want to add. I query an external API and get back a list of possible venues they actually meant, which they'll then confirm. The venue model will be populated in my local DB based upon the information in their selection.

I've implemented this a couple different ways before in hacky ways just to get it done, but now I get the sense that I'm doing too much work and not leveraging rails properly.

I get the results back turn them into an ostructs that looks like:

>> results.first
=> #<OpenStruct address="2 Maryland Plz", category_ids=[348, 342], category_labels=[["Social", "Food and Dining", "Restaurants", "American"], ["Social", "Food and Dining", "Cafes, Coffee and Tea Houses"]], country="us", factual_id="281d5b40-9d41-4eac-8367-4bc223656cb5", hours={"monday"=>[["00:00", "23:30"]], "tuesday"=>[["00:00", "23:30"]], "wednesday"=>[["00:00", "23:30"]], "thursday"=>[["00:00", "23:30"]], "friday"=>[["00:00", "23:30"]], "saturday"=>[["00:00", "23:30"]], "sunday"=>[["00:00", "23:30"]]}, hours_display="Open Daily 12:00 AM-11:30 PM", latitude=38.644621, locality="St. Louis", longitude=-90.261741, name="Coffee Cartel", neighborhood=["Central West End", "Saint Louis"], postcode="63108", region="MO", tel="(314) 454-0000", website="">

so I can simply call them like: #=> "Coffee Cartel"

I'd like to build a form that for each possible venue shows the name, address, and website, and then persists all the other information about each choice in hidden fields. What's not connecting in my brain is how to only post the selected results to the controller action.

How does associating a radio button with a specific subset of fields work in html, and then what's a clean way to do this in rails with all the form helpers? Is this possible?

Fake it.

Use data tags in your HTML and when one of the options are clicked, Javascript fills out the hidden fields in the form using the data tags on the element that was clicked. Display the data however you want. If you want radio buttons, use it for visual reasons only.

This is a non-standard form you're trying to build. The other, simpler way is to make a set of radio buttons that submit only the factual_id. You can then check the radio button you want, and then have the create action actually go look up the record and save it to the DB. This is almost always my preferred method of doing things. Then this way you can simply display the search results in the HTML however you like with p tags or whatever, you just won't submit that data in this case. You'll simply submit the factual_id.

Join the discussion

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

Join 68,099+ 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.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2022 GoRails, LLC. All rights reserved.