rails,simple_form,如何在页面加载时设置集合的选定索引? [英] rails, simple_form, how to set selected index of a collection when page loaded?

查看:39
本文介绍了rails,simple_form,如何在页面加载时设置集合的选定索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 simple_form gem,我有一个国家集合,当我选择国家时它工作正常,更新的记录将存储国家 ID,但是,当我尝试编辑记录时,未选择所选国家默认在编辑表单.

I am using simple_form gem, I have a countries collection, it work fine when I select the country, and updated record will have the country id stored, but, when I try to edit the record, the chosen country is not selected by default at edit form.

这是编辑表单中的代码:

Here is the code at edit form:

= f.input :country_id, :collection => all_countries

simple_form 不应该从数据库中查看选定的国家吗?

Shouldn't simple_form view the selected country from the db ?

推荐答案

您是否尝试过使用 :selected => 选项?

Have you tried to use the :selected => option?

:selected => selected_country_id

所以,

= f.input :country_id, :collection => all_countries, :selected => selected_country_id

这将完美运行!!!

干杯!

这篇关于rails,simple_form,如何在页面加载时设置集合的选定索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆