Producing form fields from model

I am going through the railstutorial.org book, so I am still a newbie with ruby.

My goal is to create my own multi-site framework with admin pages (including pages for administering uploaded files, users, and dynamical pages). I have been experimenting with Refinery, but it may be too difficult to modify for different purposes (not sure yet).

For now, based on the tutorial, I know how to use form_for to produce forms. The fields are handled like this:

       <%= f.label :name %>        <%= f.text_field :name, class: 'form-control' %>

What would be the neatest way to make this code more generic, so I don't have to actually write the field names here and in several other places?

I need to iterate a field list either in db schema, or in a manually-built array of fields.

- Jussi

If I understand you correclty you want to do something like:

<% fields.each do |field| %>   <%= f.label field %>   <%= f.text_field field, class: 'form-control' %> <% end %>

where fields is an array of symbols representing the names of attributes you want to accept via the form.

Personally, I prefer to have things spelled out explicitly as it's easier to see the final shape of HTML. If your goal is to save typing maybe you could consider using some kind of snippets?

Cheers Greg

First of all, thanks for admission to the list. I had to email the list owner to accomplish that.

<% fields.each do |field| %>   <%= f.label field %>   <%= f.text_field field, class: 'form-control' %> <% end %>

Ok, that's good to know.

Personally, I prefer to have things spelled out explicitly as it's easier to see the final shape of HTML. If your goal is to save typing maybe you could consider using some kind of snippets?

I am mostly thinking of admin pages. Over the years, I have put together a personal cms solution in another language (lasso). There I had for each admin page an array of arrays containing field names, field types (text, textarea, radio, checkbox, date, hidden), validations (no-html, integer, date, email, unique, length, etc.), operators (eq, lt, gt, etc.), text field sizes, and some other things. It worked well for me, and maybe I will try to create something similar now with RoR. Though maybe I could use the db/schema as the basis, and only declare additions and exceptions to the schema.

However, I am still open to alternatives, for example Refinery.

- Jussi

ri columns ri tables ri reflections ... among many others of course..

you might also want to read the many rails admin gems (eg railsadmin, activeadmin, administrate, etc), and get ideas how they do it.

best regards --botp

Just to let you know that what you are wanting to do is very difficult to get right.

As somebody that has tried 3 or 4 times and never been happy with the outcome,

I’d suggest that you do some requirements work up front to really understand the

MVP that you need for the first go around. Having it actually used by people will

bring to light a lot of stuff that will need to be addressed for the next version, so

take it in small steps so that you don’t have to re-write too much code to move forward.

To your actual question, you can easily write your own helpers that will populate the form

with the names you have stored in your database.

  • Brendon.