使用 formattastic 为带有哈希值的数组生成表单

发布于 2024-10-31 04:20:20 字数 1275 浏览 0 评论 0原文

我在格式化方面遇到困难。我将首先尝试尽可能地勾画出情况。

我有一个名为 onlinescore 的模型,它具有属性 publisherid 和其他一些基本属性。然而,它的属性之一是一个带有 urls 的数组。这些 url 是包含如下内容的哈希值:

{"url" => "http://www.example.com", "description" => "blabla"}

所以数组看起来像这样:

[{"url" => "http://www.example0.com", "description" => "blabla0"},
{"url" => "http://www.example1.com", "description" => "blabla1"},
{"url" => "http://www.example2.com", "description" => "blabla2"}]

我现在正在制作一个表单来创建新的 onlinescore ,为此我需要一种方法收集该数组的输入。我想要有 2 个字段(“url”和“description”)以某种方式映射到 urls 数组中。一旦成功,我希望用户能够一次添加多个网址,例如使用“添加新网址”按钮生成另外 2 个这些字段。

我已经尝试通过某些方式来完成此操作,例如:

#rest of form
<% @onlinescore.url.each do |link| %>
  <% form.inputs :for => link do |urls| %>
    <%= urls.input :url, :as => :url %>
    <%= urls.input :description, :as => :string %>
  <% end %>
<% end %>
#rest of form

这给出了错误:

Hash:Class 的未定义方法“model_name”

我还尝试定义一种新的输入类型 :score_url 但我也没有走得太远,因为我需要一个由 2 个字段组成的元组,而不是一个,而且我没有找到一种方法还要做吗?我研究了没有模型的表单,但我真的认为这也不是我需要的......

有人知道如何做到这一点吗?

I'm having difficulty with formtastic. I'll first try to sketch the situation as good as I can.

I have a model named onlinescore which has attributes publisher, id, and some other basic ones. One of its attributes however is an array with urls. Those urls are hashes with content like this:

{"url" => "http://www.example.com", "description" => "blabla"}

So the array looks like this:

[{"url" => "http://www.example0.com", "description" => "blabla0"},
{"url" => "http://www.example1.com", "description" => "blabla1"},
{"url" => "http://www.example2.com", "description" => "blabla2"}]

I'm now making a form to create a new onlinescore and for that I need a way to collect input for that array. I'd like to have 2 fields ("url" and "description") who get mapped into the urls array somehow. Once that works, I would want the user to be able to add several urls at once, for example with an 'add new url' button which generates another 2 of those fields.

I already tried to accomplish this on some ways for example like this:

#rest of form
<% @onlinescore.url.each do |link| %>
  <% form.inputs :for => link do |urls| %>
    <%= urls.input :url, :as => :url %>
    <%= urls.input :description, :as => :string %>
  <% end %>
<% end %>
#rest of form

which gives the error:

undefined method `model_name' for Hash:Class

I also tried to define a new input type :score_url but I didn't get far with that either because I need a tuple of 2 fields, not one and I didn't find a way to do that yet. I looked into forms without a model, but I don't really think that is what I need either...

Does anyone have an idea how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天涯沦落人 2024-11-07 04:20:20

这里的主要问题(以及“Hash:Class”的未定义方法“model_name”错误的原因)是,您尝试为其创建一组字段的每个 url 都是哈希,而不是 ActiveRecord 或 ActiveModel 模型。如果您可以将这些项目表示为模型数组,而不是哈希数组,我想您会没事的。 Formtastic 最适合模型。 Rails 是一个具有相同功能的框架。哈希在这里不起作用,抱歉,有太多的 Formtastic 和 Rails 期望能够调用每个项目。

The main problem here (and the cause of the 'undefined method `model_name' for Hash:Class' error) is that each url you're trying to create a set of fields for is a Hash, not an ActiveRecord or ActiveModel model. If you could represent those items as an array of models, instead of an array of hashes, I think you'd be fine. Formtastic works best with models. Rails is a framework that does the same. A Hash will not work here sorry, there's too much Formtastic and Rails expect to be able to call on each item.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文