ruby - Create a form in rails by reading a yaml file -


i'm trying create form reading yml file. i'm able read file i'm not sure how place items in hash code creates forms. example, yml file reads like

-  f.label: :email    f.email_field: :email  -  f.label: :name    f.text_field: :name 

i read yml file in controller this

@form_format = yaml::load(file.open('public/grant.yml')) 

and code in view

<%= form_for(:submission, url: submissions_path) |f| %>     <% @form_format.each |item| %>         <% item.each |key, value| %>            <%= key value %>            <%= key value %>         <% end %>      <% end %>    <%= f.submit "apply", class: "btn btn-primary" %> <% end %> 

i know it's <%= key value %> bit that's not correct i'm not sure how read <%= f.label: :email %> example values in hash.

the overall reason i'm trying figure out because need create many (100+) unique forms , thought best way create unique "schema" in yml file each form , read schema in create fields. if there other ways better this, i'm ears. i'm new ror i've searched extensively , haven't found much. thanks!

you're close! however, easier if yaml has label instead of f.label, so:

- label: :email   email_field: :email  - label: :name   text_field: :name 

once you've loaded yaml you'll have ruby array looks this:

[ { "label" => "email",     "email_field" => "email"   },   { "label" => "name",     "text_field" => "name"   } ] 

the trick each of hashes can use keys method names send f object.

supposing you've assigned array @form_format in controller, it'll in view:

<%= form_for(:submission, url: submissions_path) |f| %>   <% @form_format.each |item| %>     <% item.each |type, name| %>       <%= f.send(type, name) %>     <% end %>   <% end %>   ... <% end %> 

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 -