rails erb表单助手options_for_select:selected [英] rails erb form helper options_for_select :selected

查看:150
本文介绍了rails erb表单助手options_for_select:selected的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在erb中有一个编辑表单.

I have an edit form in erb.

<%= form_for @animal do |f| %>

在代码中,我可以选择以下选项:

Within the code I have a select with options:

<%= f.select :gender, options_for_select([['Mare'], ['Stallion'], ['Gelding']], :selected => :gender) %>

但是,选择未显示正确的选择值. 我可能做错了什么? 如果我对其进行硬编码,则可以使它正常工作,但这当然不是一个可行的选择.

However, the select is not showing the correct selected value. What could I be doing wrong? I can get it to work if I hardcode it but of course that is not a viable option.

推荐答案

在您的代码中,您的options_for_select()调用将所选值设置为性别",并且不会尝试使用表单对象中的值.

In your code, your options_for_select() call sets the selected value to "gender" and does not attempt to use the value from your form object.

请参阅 options_for_select() 的文档

Please see the docs for options_for_select() for usage examples.

options_for_select(['Mare', 'Stallion', 'Gelding'], f.object.gender)
options_for_select(['Mare', 'Stallion', 'Gelding'], :selected => f.object.gender)

或者,您可以执行此操作,该操作将已经使用您的表单对象的gender()值:

Alternatively, you can do this, which will already use the gender() value for your form object:

<%= f.select :gender, ['Mare', 'Stallion', 'Gelding'] %>

这篇关于rails erb表单助手options_for_select:selected的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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