1

I have a few questions relating to a Transaction object that I'm creating.

Transaction belongs_to Loan and Loan has_many Transactions.

Therefore, I setup a nested route:

resources :loans do
  resources :transactions
end

My question is: How do I pass the loan value into the Transaction's 'loan_id' field? Is this best done in the controller or as a hidden_field in the form? Does the nested route create an easy way to grab this variable?

I assumed this would be done automatically, but the field is empty when I saved it as-is.

Any help would be greatly appreciated!

Justin
  • 1,956
  • 3
  • 23
  • 34

2 Answers2

2

if you call a specific transaction, the route for a new transaction will be

loans/:loan_id/transactions/new

you could use model association like this: in your create action:

@transaction = Loan.find(params[:loan_id]).transactions.build

that way your new @transaction will be already populated with the loan_id

Andrei S
  • 6,486
  • 5
  • 37
  • 54
  • I just tried this and I got the following message: Couldn't find Loan without an ID. Do you why the :loan_id isn't being recognized? – Justin May 18 '11 at 21:15
  • make sure you have a loan in the database with the :loan_id you're trying to call. calling find on Loan will raise an exception if the loan with the id = loan_id does not exist - that should only happen if you type the url manually – Andrei S May 18 '11 at 21:19
  • The Loan exists and I'm linking to it with <%= link_to 'Lend Now', new_loan_transaction_path(@loan), :class => 'lend_button' %> Is that correct? It's still throwing the error for some reason. Thanks for the added help! – Justin May 18 '11 at 21:33
  • the link is correct (the action `new`, for a transaction in `@loan`). when is the error thrown? in the `new` or `create` action? what prints if you `puts params[:loan_id]`? is it the `id` of the called loan? – Andrei S May 18 '11 at 21:42
  • The error is thrown right after submitting the new Transaction form and it's titled ActiveRecord::RecordNotFound in TransactionsController#create. So, it's in the create method. There appears to be nothing in the params[:loan_id]. For some reason, I don't think the two are connecting. Is there something I need to add beyond what I have above to the routes perhaps? Thanks again, I really appreciate the help! – Justin May 18 '11 at 21:48
  • i tried replicating your models, relations and controllers, and for test purposes, all i have in create is `@transaction = Loan.find(params[:loan_id]).transactions.build(params[:transaction])` after which i call `save`. and it works. does running `rake routes` give a route like `new_loan_transaction GET /loans/:loan_id/transactions/new(.:format) {:action=>"new", :controller=>"transactions"}` ? if the answer is yes, then i'm starting to go out of ideas :) – Andrei S May 18 '11 at 22:21
  • Thanks, my friend! It works now! I'm not sure what exactly caused it to work, because I made multiple changes at once, but it works! I really appreciate all of the help!!! – Justin May 18 '11 at 23:41
0

Consider adding a before_filter to your controller and having it call a private method to grab the :id across all actions. Place this at the top of your transactions controller:

before_filter :load_loan

And then after all the actions, add:

private
def load_loan
  @loan.find(params[:loan_id])
end

Use it like this in your new action:

@transaction = @loan.transactions.build
Chadwick
  • 12,555
  • 7
  • 49
  • 66
Paul Simpson
  • 2,504
  • 16
  • 28