【问题标题】:Is it possible to bind an attribute to a forms_helper.select in a partial?是否可以在部分中将属性绑定到 forms_helper.select?
【发布时间】:2011-03-21 15:35:25
【问题描述】:

在 Rails 视图中,我试图显示一个<select> 下拉列表,其中包含许多具有受限值的不同字符串字段。

我一直在尝试使用部分(如下)执行此操作,但当前值未在 <select> 列表中选择。

  • 是否可以部分执行此操作?如果有,怎么做?
  • 有更好的方法吗?

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>

【问题讨论】:

    标签: ruby-on-rails views partial-views multi-select partials


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢。这比我预期的更令人费解,但如果它有效,它就有效。希望我会在这周晚些时候尝试一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-30
    • 2011-04-01
    • 2015-01-23
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多