是否可以将属性绑定到部分中的 forms_helper.select ?
在 Rails 视图中,我尝试显示一个
我一直在尝试使用部分(如下)来执行此操作,但未在
- 可以部分地做到这一点吗?如果是这样,怎么办?
- 有更好的方法吗?
edit.html.erb:
<% form_for(@my_class) do |f| %>
<%= render :partial => "select", :locals => { :attribute_name => :blah, :f => f } %>
<% end %>
_select.html.erb:
<p>
...
<%= f.label attribute_name %><br />
<%= f.select attribute_name, [:option_a,:option_b,:option_c], { :selected => attribute_name } %>
...
</p>
In a Rails view, I'm trying to show a <select>
drop-down list for a number of different string fields with restricted values.
I've been trying to do this with a partial (below), but the current value is not being selected in the <select>
list.
- Is it possible to do this in a partial? If so, how?
- Is there a better approach to take?
edit.html.erb:
<% form_for(@my_class) do |f| %>
<%= render :partial => "select", :locals => { :attribute_name => :blah, :f => f } %>
<% end %>
_select.html.erb:
<p>
...
<%= f.label attribute_name %><br />
<%= f.select attribute_name, [:option_a,:option_b,:option_c], { :selected => attribute_name } %>
...
</p>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信所选选项是根据值而不是属性名称进行检查的。
这可能对你有用,但我还没有测试过:
I believe the selected option checks based on the value, not the name of the attribute.
This may work for you, but I have not tested it out: