ruby on rails - How to find which fields in a model should be set? -


i have ror application, , scaffold generation gave me new , edit actions model. in views, there _form.html.erb. form has inputs 2 fields happen attr_accessible no inputs fields created_at, updated_at , id.

i want generate custom other forms , views models. want know how programmatically find fields should set manually, , fields set rails. if have model widget, widget.columns gives fields.

how rails scaffold generation know fields put in form. also, there code determines how "created_at" , "updated_at" set? or done db?

update:to clarify, want things specifically:

  1. i generating forms uploading large number of rows/entities. need know fields include in form. widget.accessible_atributes fields set manually?

  2. i need know fields include automatically. if use new method of model, created_at, updated_at, , id set automatically. want load 1000's of rows in table , sql load file command , think need set created_at , updated_at not id. how know fields set. looking hypothetical method like

    >> widget.auto_columns # returns {:created_at => '04/12/2013 9:29pm', :updated_at => '04/12/2013'} 

update 2: trying see how scaffolding done see in gems/railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb, code uses attributes array contains attributes show in form.

<% attributes.each |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 attributes array being generated in scaffold_generator.rb thor's argument method.

how rails scaffold generation know fields put in form.

when generating scaffold, generate this::

rails generate scaffold tablename attribute:type_of_attribute attribute2:type_of_attribute 

and can concatenate many attributes wish.

also, there code determines how "created_at" , "updated_at" set? or done db?

the timestamps (created_at, updated_at) , id set rails automatically.

they added database (unless want remove timestamps in migration files manually, id stay).

also can set fields in models (tables) automatically generated, have write code that.

about accessible attributes, although don't know use of it:

photo.accessible_attributes.each { | attribute | puts attribute if attribute.length > 0 } 

for reason gives me first value "", escaped that. more on attributes , accessibility , counting columns:

retrieving rails model column names

how can discover accessible attribute names model instance?


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -