如何从 formattastic 的 :select 框中获取标签字符串值

问题描述 投票:0回答:2

我在 Rails 3.2 应用程序中使用 formattastic gem 的部分内容有以下内容

<%= f.semantic_fields_for :bucket do |bucket| %>
                       <%= bucket.inputs do %>
                               <%= bucket.input :bucket_name, :collection => @buckets,
                               :include_blank => false %>
                               <%= bucket.input :sub_directory, :collection =>
@buckets.first.paths,
                               :include_blank => false %>
                       <% end %>
               <% end %>

现在在我的控制器中我可以获得 :bucket_name 并且值是 == 到一个整数,在我的例子中,我的集合中只有一项,但它给我的值为 2。我的猜测是,这是该对象的 ID 值。

获取对象的实际 :name 字符串值很重要 是在集合中选择的。我不知道该怎么做。所以 假设我选择的项目有一个标签“我的标签”,它是第三个 集合中的项目。我如何获取“我的标签”的值。

ruby-on-rails-3 formtastic
2个回答
1
投票

:member_value 和 :member_label 从 Formtastic v3 开始已弃用。

最简单的方法是修改传递到输入中的集合。请参阅 formattastic github 页面的示例

 f.input :author,  :as => :select,      :collection => Author.pluck(:first_name, :id)

这里的first_name是标签,id是选择选项的值


-1
投票

默认情况下,选择输入将使用模型的

id
属性作为
value
标签的
<option>
属性,并将对象上的各种方法与选项标签的内容联系起来,例如
to_label
name
to_s

您可以分别使用

:member_value
:member_label
选项进行更改(在旧版本中这些选项称为
:value_method
:label_method

每个选项的详细信息位于选择输入的文档中:

http://rdoc.info/github/justinfrench/formtastic/Formtastic/Inputs/SelectInput

© www.soinside.com 2019 - 2024. All rights reserved.