1

We used a migration to add a new field to our Posts table called new_choice:integer with :default => 1

On the edit form we made it a radio button with 3 choices A,B,C represented by values 1,2,3

When a NEW record is created, the "A" button will be selected by default.

However, when an already-existing record is edited, the current value is nil so NO radio button is selected.

Is there a way to have the erb form check a particular button (A/1) if the field value sis not the "valid" choices of 1,2,3?

2 Answers 2

3

This should work:

<%= f.radio_button :post, choice, :checked => @post.choice == choice ? true : false %>
Sign up to request clarification or add additional context in comments.

1 Comment

that looks close... I think I need to tweak that, I dont think it handles the case I want which is IF the field value is nil then I need to choose one to be checked, otherwise the buttons matching the current value is checked (which happens automatically)... probably something like :checked => (pseudo code: true if field is nil OR if field = value, else false)
1

The last option in radio button method will set the default, selected value:

radio_button("post", "new_choice", "1")

Comments

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.