0

I have a field in my model called isTransfer:

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

I create a controller that should act like action: :new, but only for a transfer call 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

When I see the new transfer in my view form, before posting, I can see that isTransfer is set to true. But when I post, it always goes into the DB as false. The other fields (description and amount) do not change - they go in as expected.

Here is the model:

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
Cagilla
  • 13
  • 4

2 Answers2

0

I would suggest you do the presets in the create controller method rather than new

Also, you can add a "!" to make the save method return you any errors from the console: e.g

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

    end
end
phil88530
  • 1,499
  • 3
  • 19
  • 27
  • I did find a post about setting attr_accessible and thought that must be it, but I already had it set. Posted the model above – Cagilla Oct 15 '12 at 11:41
  • I am not sure how that would work. When I press the new transfer button, I wanted a new transaction, that also sets the bit isTransfer. Then the user would update the record and post. Doesn't the controller go from new to create after the submit is pressed? Or is the issue that I did not include isTransfer as an editable field on the forms? – Cagilla Oct 16 '12 at 01:29
  • thank you for your time. I realized the answer as I wrote the last comment. – Cagilla Oct 16 '12 at 01:55
  • you are welcome, the issue is that the new method doesn't help much for your presets. you should either put it in a hidden field as a input(as your best answer is), or make your default setup in the create method under your controller rather than the new method – phil88530 Oct 17 '12 at 21:25
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) %>
Cagilla
  • 13
  • 4