type information is lost in simple_fields_for?

96 views
Skip to first unread message

kedar mhaswade

unread,
Jan 20, 2012, 9:57:55 AM1/20/12
to plataformatec-simpleform
I have a model with an integer field, time_limit:

class Test < ActiveRecord::Base
attr_accessible :time_limit # declared to be :integer in migration
# omitted
end

When I generate a simple_form for an instance of Test,
<%= f.input :time_limit %>

renders the text-field with counter control (small-up/down arrows to increment/decrement integer value) as it gives that field a class: integer and numeric.
This is expected.

However, when I have an instance of this model Test inside another model say Exam and I use simple_fields_for
like:
<%= simple_form_for @exam do |f| %>
....
      <%= f.simple_fields_for :test do |test_form| %>
          <%= test_form.input :time_limit %>
      <% end %>
<%end %>

I find that the same field is now rendered with string class losing the counter control! Even if I use :input_html => "numeric integer", since in the rendered HTML (:class = "string numeric integer"), "string" prevails, the counter control is lost.

What am I doing wrong?

Regards,
Kedar


Carlos Antonio da Silva

unread,
Jan 20, 2012, 10:43:38 AM1/20/12
to plataformate...@googlegroups.com
Do you have "accept_nested_attributes_for" configured for test in the Exam model? It seems the "fields_for :test" is not being binded to an object, which means it cannot verify the column type to render the proper field, defaulting to string.
--
At.
Carlos Antonio

kedar mhaswade

unread,
Jan 21, 2012, 8:42:27 AM1/21/12
to plataformate...@googlegroups.com
Thanks Carlos, I will try out accept_nested_attributes_for. I somehow think that there's something weird going on. Let me investigate.
I worked around the problem by using :as => :integer.

Carlos Antonio da Silva

unread,
Jan 21, 2012, 9:12:35 AM1/21/12
to plataformate...@googlegroups.com
Ok, please let us know if you find anything. 

fields_for can receive an object to work with - basically the same way as form_for does, and SimpleForm uses the object to reflect column information. Without object, no column information, thus all fields will be considered string.

You can always give an extra argument to fields_for with an object if you want, for instance:

  f.fields_for :test, Test.new

And of course, force the field type using the :as, just like you did :)
--
At.
Carlos Antonio
Reply all
Reply to author
Forward
0 new messages