Rails mongoid dynamic fields - no method error

2019-09-18 03:08发布

ENV:

Rails-3.2.12

Ruby-1.9.3

Mongoid-3.1.1

I have model:

class Item
   include Mongoid::Document
   field :name, type: String
   field :type, type: String
end

but if I try to add dynamic field in view, lets say "color", i get an undefined method error.

allow_dynamic_fields: true is enabled in the config file.

_form.html.erb:

<%= form_for(@item) do |f| %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
<div class="field">
  <%= f.label :type %><br />
  <%= f.text_field :type %>
</div>
<div class="field">
  <%= f.label :color %><br />
  <%= f.text_field :color %>
</div>

everything works fine if I try to edit item which already have color attribute. I need to add several dynamic attributes which depends on item.type but without something like this:

<% if @item[:color] %>
  <%= f.text_field :color %>
<%else%>
  <%= text_field_tag 'item[color]' %>
<% end %>

EDIT:

Error:

NoMethodError in Items#new

Showing /app/views/items/_form.html.erb where line #31 raised:

undefined method `color' for # Extracted source (around line #31):

28:     <%= f.number_field :type %>
29:   </div>
30:    <%= f.label :color %><br />
31:     <%= f.text_field :color %>
32:     <div class="actions">
33:       <%= f.submit %>
34:     </div>

1条回答
手持菜刀,她持情操
2楼-- · 2019-09-18 03:39

Mongoid docs says:

"If the attribute does not already exist on the document, Mongoid will not provide you with the getters and setters and will enforce normal method_missing behavior. In this case you must use the other provided accessor methods: ([] and []=) or (read_attribute and write_attribute). "

The easiest thing you can do is to set 'color' in your controller #new method using write_attribute or []=

@item['color'] = ''

Or you can just dynamically add 'color' attribute to your new Item singleton class:

class << @item
  field :color, type: String
end
查看更多
登录 后发表回答