通过阅读yaml文件在rails中创建表单

通过阅读yaml文件在rails中创建表单

本文介绍了通过阅读yaml文件在rails中创建表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过阅读一个yml文件来创建一个表单。我能够正确读取文件,但我不确定如何将项目散列正确放置到创建表单的代码中。
例如,我的yml文件读取的内容类似于

   -  f.label::email 
f。 email_field::email

- f.label::name
f.text_field::name

我在我的控制器中读取yml文件,如下所示:

  @form_format = YAML :: load File.open('public / grant.yml'))

并且我的视图中的代码是像这样

 <%= form_for(:submission,url:submissions_path)do | f | %GT; 
<%@ form_format.each do | item | %GT;
<%item.each do | key,value | %GT;
<%=键值%>
<%=键值%>
<%end%>
<%end%>
<%= f.submitApply,class:btn btn-primary%>
<%end%>

我知道这是<%=键值%>位,这是不正确的,但我不知道如何让它读取散列值中的<%= f.label::email%>。

我试图弄清楚这一点的总体原因是因为我需要创建许多(100+)独特的表单,并且我认为我能做到这一点的最佳方式是在每个表单的yml文件中创建一个唯一的模式并读取模式以创建字段。如果还有其他更好的方法可以做到这一点,我就会全神贯注。我对RoR相当陌生,但是我已经广泛搜索并没有发现太多东西。谢谢!

解决方案

你很亲密!然而,如果你的YAML只有 label 而不是 f.label ,那么这会容易得多, p>

   -  label::email 
email_field::email

- label::name
text_field :: name

一旦你加载了这个YAML,你将拥有一个Ruby数组看起来像这样:

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

诀窍在于,对于每一个我们可以使用这些键作为方法名称发送给 f 对象。



假设您已经指定了数组到 @form_format 在你的控制器中,它会在你的视图中看起来像这样:

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


I'm trying to create a form by reading a yml file. I'm able to read the file properly but I'm not sure how to place the items in the hash properly into the code that creates the forms.For example, my yml file reads something like

-  f.label: :email
   f.email_field: :email

-  f.label: :name
   f.text_field: :name

I read the yml file in my controller like this

@form_format = YAML::load(File.open('public/grant.yml'))

and the code in my view is something like this

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

I know that it's the <%= key value %> bit that's not correct but I'm not sure how to get it to read <%= f.label: :email %> for example from the values in the hash.

The overall reason I'm trying to figure this out is because I need to create many (100+) unique forms and I thought the best way I could do this is to create a unique "schema" in a yml file for each form and read the schema in to create the fields. If there are other ways that are better to do this, I'm all ears. I'm fairly new to RoR but I've searched extensively and haven't found much. Thanks!

解决方案

You're very close! However, this will be much easier if your YAML has just label instead of f.label, like so:

- label: :email
  email_field: :email

- label: :name
  text_field: :name

Once you've loaded this YAML you'll have a Ruby array that looks like this:

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

The trick is that for each of the hashes we can use the keys as method names to send to the f object.

Supposing you've assigned the array to @form_format in your controller, it'll look like this in your view:

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

这篇关于通过阅读yaml文件在rails中创建表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 20:11