0

我的模型中有一个名为 isTransfer 的字段:

class AddTxfrColumnsToTransaction < ActiveRecord::Migration
  def change
    add_column :transactions, :isTransfer, :boolean
    add_column :transactions, :transferAccount_id, :integer
  end
 end

我创建了一个控制器,它应该像 action::new 一样,但仅用于传输调用 new_transfer:

def new_transfer
  account = Account.find(params[:account_id])
  @transaction = account.transactions.build
  @transaction.description = "Transfer"
  @transaction.isTransfer = true
  @transaction.amount = 100

  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @transaction }
  end
end

当我在我的视图表单中看到新的转移时,在发布之前,我可以看到 isTransfer 设置为 true。但是当我发布时,它总是以错误的形式进入数据库。其他字段(描述和金额)没有改变 - 它们按预期进入。

这是模型:

class Transaction < ActiveRecord::Base
  attr_accessible :account_id, :amount, :check, :date, :description, :is_cleared, :note, :category, :isTransfer, :transferAccount_id
  validates_presence_of :amount, :date

  belongs_to :account, class_name: 'Account'
  belongs_to :transferAccount, class_name: 'Account'

end
4

2 回答 2

0

Ok, this is probably a complete noob mistake. I had originally believed that if I set a value in the controller (as part of new_transfer action) that it would persist to the create action after submit. My mistake was that in not referencing it at all on the new_transfer form, it was never passed back to the Create action as a param. By adding the following to my new_transfer form, isTransfer now updates in the create action:

<%= f.hidden_field(:isTransfer) %>
于 2012-10-16T01:55:07.840 回答
0

我建议您在创建控制器方法中进行预设,而不是新建

此外,您可以添加一个“!” 使 save 方法从控制台返回任何错误:例如

def create
    ###do your preset methods here
    if(@transaction.save!)

    end
end
于 2012-10-15T11:20:48.560 回答