在Rails

时间:2018-01-19 08:52:53

标签: ruby-on-rails

在我的应用中,用户可以创建一组用于营销活动的代码。因此,我想要一个表单,用户可以在其中键入要创建的代码量,然后系统应该生成那么多代码并将它们保存到数据库中。

code.rb

class Code < ActiveRecord::Base
  belongs_to :form

   attr_accessor :amount
end

_form.html.erb

<%= form_for [:customer, @code] do |f| %>

  <% if @code.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@code.errors.count, "error") %> prohibited this code from being saved:</h2>

      <ul>
      <% @code.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :amount %><br>
    <%= f.text_field :amount %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

我现在要做的是告诉控制器:

  1. 拿:金额
  2. 创建尽可能多的SecureRandom.hex(3):amount指定
  3. 将所有代码保存到数据库(表中代码的列名为“code”)
  4. 但是,我完全不知道如何实现这一目标......

0 个答案:

没有答案
相关问题