its easy but i forgot all

my models

borrower ----- has_many :loans
loan ----- belongs_to :borrower

my loans _controller

  def new
    @borrower = Borrower.find(params[:borrower_id])
      logger.debug ' @borrower.id'
      logger.debug @borrower.id
  @loan = Loan.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml { render :xml => @loan }
    end
  end

  def create
      logger.debug ' @borrower.id'
      logger.debug @borrower.id
  @loan = Loan.new(params[:loan])
    respond_to do |format|
      if @loan.save
        format.html { redirect_to(@loan, :notice => 'Loan was
successfully created.') }
        format.xml { render :xml => @loan, :status
=> :created, :location => @loan }
      else
        format.html { render :action => "new" }
        format.xml { render :xml => @loan.errors, :status
=> :unprocessable_entity }
      end
    end
  end

in my views i am passing value like this

    <%= link_to 'New Loan', :controller => 'loans', :action =>
'new', :borrower_id => @borrower.id %>

every thing working fine in Action new logger.debug
@borrower.id Gives id
but on create it does not shows anything
Where i am wrong and did something i missed i wanna save @borrower.id
in @loans.borrower_id
Please help thanks in advance

If the issue is in the create action please provide the corresponding
code. Model code would be helpful too.

Luke


I have two modelds

  1. loan model belongs_to :borrower

  2. borrower model has_many:loans

In my borrower Show method i wrote

<%= link_to 'Request New Loan', :controller => 'loans', :action => 'new', :id => @borrower.id %>

my loans_controller have this
def new
@borrower = Borrower.find(params[:id])
@loan = Loan.new

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @loan }

end
end

def create

@loan = Loan.new(params[:loan])

@borrower = Borrower.find(params[@borrower_id])

respond_to do |format|

 if @loan.save

   format.html { redirect_to(@loan, :notice => 'Loan was successfully created.') }

   format.xml { render :xml => @loan, :status => :created, :location => @loan }

 else
   format.html { render :action => "new" }

   format.xml { render :xml => @loan.errors, :status => :unprocessable_entity }

 end

end
end

On loan create Giving error can not find borrower without an id but on new method’si am getting borrower id prints very well .

where i am wrong
































I have two modelds

  1. loan model belongs_to :borrower

  2. borrower model has_many:loans

In my borrower Show method i wrote

<%= link_to 'Request New Loan', :controller => 'loans', :action => 'new', :id => @borrower.id %>

my loans_controller have this
def new
@borrower = Borrower.find(params[:id])

@loan = Loan.new

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @loan }

end
end

def create

@loan = Loan.new(params[:loan])

@borrower = Borrower.find(params[@borrower_id])

Compare this line to the equivalent line in the new action. What is the @ doing inside the []? I don’t know whether it should be borrower_id or not, that depends on what params are being passed. Look in the log to see what the params are when you click on create.

Colin