Andrew James Andrew James - 3 months ago 21
Ruby Question

ActionController::ParameterMissing in LoansController#new

I know there are lots of these errors discussed and I have read many of them with no success so this is not a first step by posting. I am a developer but new to Ruby so any guidance would be appreciated thanks!

I have 2 models Loan and Growthyear and Loan has_many :growthyears

Existing loans are displaying and I can edit them but trying to create a new Loan now gets this error ever since I added the nested association growthyears.

I can post the new.html.erb but I don't believe it is even getting that far.

ActionController::ParameterMissing in LoansController#new
param is missing or the value is empty: loan

Extracted source (around line #249):

value
else
raise ParameterMissing.new(key)
end
end


loan.rb

class Loan < ActiveRecord::Base
has_many :growthyears
accepts_nested_attributes_for :growthyears, reject_if: proc { |attributes| attributes['growth'].blank? }, allow_destroy: true
end


growthyear.rb

class Growthyear < ActiveRecord::Base
belongs_to :loan
validates_presence_of :growth
end


loans_controller.rb

class LoansController < ApplicationController
def show
@loan = Loan.find(params[:id])
end

def home
end

def destroy
Loan.find(params[:id]).destroy
flash[:success] = "Loan (" + params[:id] + ") deleted " + Time.now.localtime("+10:00").to_s
redirect_to loans_url
end

def index
@loans = Loan.paginate(page: params[:page], :per_page => 5)
end

def new
@loan = Loan.new(user_params)
@loan.growthyears.build
end

def create
@loan = Loan.new(user_params) # Not the final implementation!
if @loan.save
flash[:success] = "(" + @loan.id.to_s + ") " + @loan.name + " New Loan Saved! " + Time.now.localtime("+10:00").to_s
render 'edit'
else
render 'new'
end
end

def update
@loan = Loan.find(params[:id])
@loan.yeargrowth = params[:yearlygrowth]
if @loan.update_attributes(user_params)
flash[:success] = "(" + @loan.id.to_s + ") " + @loan.name + " Loan updated " + Time.now.localtime("+10:00").to_s
end
redirect_to edit_loan_path(@loan.id)
end

def edit
@loan = Loan.find(params[:id])
flash[:success] = "(" + @loan.id.to_s + ") " + @loan.name + " Loan retrieved " + Time.now.localtime("+10:00").to_s
end

private

def user_params
params.require(:loan).permit(:name, :CCV, :Loan, :IRV, :ODV, :period, :simLowInt, :simHighInt, :bSimActive, :LoanBook, growthyears_attributes: [:growth, :id, :_destroy] )
end
end

Answer

you should declare only Loan.new, because in new you only initliazie this object so instead this

 def new
    @loan = Loan.new(user_params)
    @loan.growthyears.build
  end

put this:

 def new
    @loan = Loan.new
    @loan.growthyears.build
  end