在Rails应用

时间:2016-03-14 22:38:32

标签: ruby-on-rails

在过去的几天里,我一直在努力解决这个问题,非常感谢一些帮助。一个有两个模型的Rails应用程序:

  • 帐户 - 名称[string],credit [boolean],active [boolean]
  • 余额 - 余额[小数],日期[日期],帐户[整数]

balance处创建新的http://localhost:3000/balances/new时,表单如下所示:

enter image description here

如何让用户能够同时创建多个余额?日期字段应该只有一个文本框应该用于创建的所有余额记录,但是表单上应该有多个帐户下拉列表和余额文本框。

我试过查找嵌套表格,但我很挣扎。

CODE

模式

  create_table "accounts", force: :cascade do |t|
    t.string   "name"
    t.boolean  "credit"
    t.boolean  "active"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "balances", force: :cascade do |t|
    t.decimal  "balance"
    t.date     "date"
    t.integer  "account_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

account.rb

class Account < ActiveRecord::Base
  has_many :balances

  validates :name, presence: true, length: { maximum: 250 },
                  uniqueness: { case_sensitive: false }
end

balance.rb

class Balance < ActiveRecord::Base
  belongs_to :account
  #default_scope -> { order(date: :desc) }

  validates :account, presence: true, length: { maximum: 250 }
end

accounts_controller.rb

....
def new
  @account = Account.new
end

def create
  @account = Account.new(account_params)

  respond_to do |format|
    if @account.save
      format.html { redirect_to accounts_path, notice: 'Account was successfully created.' }
      format.json { render :show, status: :created, location: @account }
    else
      format.html { render :new }
      format.json { render json: @account.errors, status: :unprocessable_entity }
    end
  end
end
....

balances_controller.rb

....
def new
  @balance = Balance.new
end

def create
  @balance = Balance.new(balance_params)

  respond_to do |format|
    if @balance.save
      format.html { redirect_to balances_path, notice: 'Balance was successfully created.' }
      format.json { render :show, status: :created, location: @balance }
    else
      format.html { render :new }
      format.json { render json: @balance.errors, status: :unprocessable_entity }
    end
  end
end
....

余额/ _form.html.erb

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

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

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

  <div class="field">
    <%= f.label :account_id %><br>
    <%= f.collection_select(:account_id, Account.all.where(active: true).order('name ASC'), :id, :name,{})%>
  </div>

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

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

1 个答案:

答案 0 :(得分:1)

所以,在我看来,你的挑战有两个部分。

  1. 创建具有多个余额的表单,
  2. 处理控制器中的多个余额。
  3. 我不打算详细介绍。希望这会指出你正确的方向......

    在您的余额表单上,使用form_tag代替form_for,这样您就可以代表多个余额(而不是绑定到单个余额实例)。创建余额部分(包括帐户名称(作为纯文本,而不是下拉列表),余额输入和account.id隐藏字段)。使用余额部分迭代所有活动帐户,以创建适当的输入。日期字段将位于迭代器之外,以便您获得一个日期输入。你可以摆弄所有这些,这样你的余额都会以一个参数哈希结束。

    在您的控制器中,获取余额的参数哈希值,然后对其进行迭代,以使用每个新余额的一个日期值创建余额。

    我知道这是非常高的水平。我道歉但是,写出所有代码的时间远远超过我现在的时间。希望这可以帮助您实现这一目标。

相关问题