LoConController中的ActionController :: ParameterMissing #new

时间:2016-08-26 06:57:37

标签: ruby-on-rails ruby

我知道有很多这些错误已经讨论过了,我已经阅读了很多这些错误并没有成功,所以这不是发布的第一步。我是开发人员,但对Ruby很陌生,所以任何指导都将不胜感激!

我有2个模型Loan和Growthyear and Loan has_many:growthyears

现有贷款正在显示,我可以对其进行编辑,但是自从我添加嵌套关联增长年以来,尝试创建新贷款现在就会出现此错误。

我可以发布new.html.erb,但我认为它甚至没有达到那么远。

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

1 个答案:

答案 0 :(得分:0)

你应该只声明Loan.new,因为在新的你只能初始化这个对象而不是这个

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

把这个:

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