开发者

Filters Options for collection_select

开发者 https://www.devze.com 2023-03-05 23:48 出处:网络
I have a model called Metadatum (silly, I know) which has two fields, key and value. A key is prefixed with it\'s type followed by an underscore. The possible types are modifier_, option_ oruom_ (unit

I have a model called Metadatum (silly, I know) which has two fields, key and value. A key is prefixed with it's type followed by an underscore. The possible types are modifier_, option_ or uom_ (unit of measure) which is then followed by a user-defined key. For example: uom_ounce.

I define the types in a class method of Metadatum:

def self.types
    { 'Modifier' => 'modifier', 'Option' => 'option', 'Unit of Measure' => 'uom'}
end

I parse out my static types from the user-defined keys using this instance method:

def parsed_key
    self.key.split('_', 2).last
end

My goal is to present a select box for each type. Here's how I'm trying to do accomplish it:

<% Meta开发者_StackOverflowdatum.types.each do |type| %>
    <%= f.fields_for :item_metadata do |builder| %>
        <%= cell((builder.label :fk_metadatum_id, type.first),   (builder.collection_select(:fk_metadatum_id, Metadatum.where(["key LIKE ?", "%#{type.last}%"]).first, :id, :parsed_key, options ={:prompt => "- #{type.first}"}) ))%>
    <% end %>
<% end %>

When I try to render the page in my browser, I get this error message:

undefined method `map' for #<Metadatum:0x1c9c68b38>

Here's the offending code:

Metadatum.where(["key LIKE ?", "%#{type.last}%"]).first, :id, :parsed_key, options ={:prompt => "- #{type.first}"

Originally, I had Metadatum.all instead and the page rendered, but that isn't the desired result.

Is there a simple way to filter options for a select box?


A mindless programming mistake.

I was calling .first on the collection. I removed it, and now it works.

0

精彩评论

暂无评论...
验证码 换一张
取 消