I have a RoR application, and the scaffold generation gave me new and edit actions for my model. In the views, there is a _form.html.erb. This form has inputs for two fields that also happen to be attr_accessible but no inputs for fields like created_at
, updated_at
and id
.
I want to generate some custom other forms and views for all my models. I want to know how to programmatically find the fields that should be set manually, and which fields are set by Rails. If I have a model Widget, then Widget.columns gives all the fields.
How does Rails scaffold generation know which fields to put in the form. Also, is there code which determines how "created_at" and "updated_at" are set? Or is this done by the DB?
Update:To clarify, I want to do to things specifically:
I am generating forms for uploading a large number of rows/entities. So I need to know which fields to include in the form. Is it always
Widget.accessible_atributes
the fields to be set manually?I need to know which fields to include automatically. If I use the new method of the model,
created_at
,updated_at
, andid
are set automatically. I want to load 1000's of rows in the table I will do something like and SQL load file command and I think I need to set created_at and updated_at but not id. How do I know which fields to set. I am looking for some hypothetical method like>> Widget.auto_columns # returns {:created_at => '04/12/2013 9:29pm', :updated_at => '04/12/2013'}
Update 2: Trying to see how scaffolding is done I see that in gems/railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb, the code uses something like an attributes array that contains only the attributes to show in the form.
<% attributes.each do |attribute| -%>
<div class="field">
<%%= f.label :<%= attribute.name %> %><br />
<%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
</div>
<% end -%>
<div class="actions">
<%%= f.submit %>
</div>
<%% end %>
I think the attributes array is being generated in scaffold_generator.rb with thor's argument method.