是否可以将属性绑定到部分中的 forms_helper.select ?

发布于 2024-09-12 00:16:41 字数 679 浏览 3 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热风软妹 2024-09-19 00:16:41

我相信所选选项是根据值而不是属性名称进行检查的。

这可能对你有用,但我还没有测试过:

<%= f.select attribute_name, [:option_a,:option_b,:option_c], { :selected => @my_class.send(attribute_name) } %>

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:

<%= f.select attribute_name, [:option_a,:option_b,:option_c], { :selected => @my_class.send(attribute_name) } %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文