【问题标题】:how to define a model which is an array (not in database!)如何定义一个数组模型(不在数据库中!)
【发布时间】:2026-01-04 01:40:02
【问题描述】:

我想将名为“adding”的表单中的数据存储在一个数组中——我想根据这个数组中的数据生成其他表单,所以我认为我不必在这里使用数据库(我错了吗?) .我应该如何在“添加”的模型中定义一个数组?这就是我的表单的样子:

<h2>Add new data</h2>
Please select, what kind of data you want to add:<br /><br />
<%= simple_form_for :adding do |f| %>

  <%= f.input :first_name, :collection => 0..10 , :prompt => "How many?" %>

  <%= f.input :last_name, :collection => 0..10 , :prompt => "How many?" %>

  <%= f.input :city, :collection => 0..10 , :prompt => "How many?" %>

  <%= f.input :postal, :collection => 0..10 , :prompt => "How many?" %>

  <%= f.input :street, :collection => 0..10 , :prompt => "How many?" %>

  <%= f.input :job, :collection => 0..10 , :prompt => "How many?" %>

  <%= f.input :role, :collection => 0..10 , :prompt => "How many?" %>

  <%= f.button :submit, 'next step', :style => "margin-top: 20px;" %>
<% end %>

请帮忙:/

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 model-view-controller


    【解决方案1】:

    当您单击提交按钮时,您会将输入中的值作为参数传递给您定义的操作和控制器。您可以在该操作中处理并执行一些工作,然后渲染将根据之前的输入创建的新表单。

    【讨论】:

    • 这种基本方法的问题是验证。一般来说,最好有纤细的控制器,从而在模型中提取逻辑
    【解决方案2】:

    Check this railscast,这是一个很好的活生生的例子。

    请记住:在 Ruby 中,您不必键入变量。

    【讨论】: