简体   繁体   中英

Rails 3, add radio button with migration, how set a default to existing data?

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?

这应该工作:

<%= f.radio_button :post, choice, :checked => @post.choice == choice ? true : false %>

单选按钮方法中的最后一个选项将设置默认的选定值:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM