Search code examples
ruby-on-railsuser-inputbefore-savelov

rails use form input value before save


I have a form in rails with input values, and 1 of the values is a LOV (List of Values) and a second input field is also a LOV but depends on the input of the other field. The input value of the first field is not saved in between. So I need to use that field value before it is saved. So, the example: I choose a company from the list of values of all companies for the field supplier, the second field supplier_address will be a LOV with all the addresses of that company, so the LOV of the second field is dependent on the value chosen in the first field, company.

What I tried:

  def new
    @purchase_requisition = PurchaseRequisition.new
    @purchase_requisition.number = find_next_user_value("Purchase_Requisition")
    #@purchase_requisition.supplier_address_id =  PurchaseRequisition.new.purchase_requisition_params[:supplier_id]
    @purchase_requisition = PurchaseRequisition.new(purchase_requisition_params)

    respond_to do |format|
      @purchase_requisition.supplier_address_id = PurchaseRequisition.new.purchase_requisition_params[:supplier_id]
    end
  end

but I still get the error:

param is missing or the value is empty: purchase_requisition

Can someone please help me? Thank you!!!


Solution

  • The error you're encountering isn't being caused by the code you've provided. You're probably using strong parameters and have a method like this:

    def purchase_requisition_params
       params.require(:purchase_requisition).permit(# some list of attributes #)
    end
    

    The problem is that params[:purchase_requisition] doesn't exist. Probably because the form_for in your view isn't referencing a purchase_requisition object. Try adding as: to your form_for to send your params under that param key:

    form_for @requisition, as: :purchase_requisition, ....
    

    Otherwise, you'll have to post more details about your view and controller to help isolate the issue you're having.

    Also, in your controller code you want:

    PurchaseRequisition.new(purchase_requisition_params[:supplier_id])
    

    Instead of:

    PurchaseRequisition.new.purchase_requisition_params[:supplier_id]