Whitelist parameters from a HTML table (Dynamic) and Save to JSONB format.

Hello,

Myself Mukheem. I have just started working with Ruby on Rails.

I am trying to create a small billing application where a receptionist can bill the items and give the receipt to customer.

Now, I wanted to white list all the items from HTML table from front end and save them to DB, JSONB format in a record.

White listing a text_field is easy. but I dont know how to do it when the same is present in a HTML table. Some how i managed to reach till this point. Rest of the details are as follows.

HTML Code

<tr>
            <td><input id="order[orderplaced][:itemname][0]" name="order[orderplaced][:itemname][0]" type="text" /></td>
            <td><input id="order[orders_attributes][0][quantity]" name="order[orderplaced][:quantity][0]" type="text" /></td>
            <td><input id="order[orders_attributes][0][unitprice]" name="order[orderplaced][:unitprice][0]" type="text" /></td>
            <td><input id="order[orders_attributes][0][tax]" name="order[orderplaced][:tax][0]" type="text" /></td>
            <td><input id="order[orders_attributes][0][discount]" name="order[orderplaced][:discount][0]" type="text" /></td>
            <td><input id="order[orders_attributes][0][itemtotalprice]" name="order[orderplaced][:itemtotalprice][0]" type="text" /></td>
        </tr>

Only one row is static, rest rows are added dynamically on the fly as per need(Java script is used). I came to know that there are a few changes which needs to be made in front end code as well. but I am not sure in which format rails expect. :frowning:

Controller Code

class OrdersController < ApplicationController

    def new
        @order=Order.new
    end

    def create
        @order=Order.new(fixed_order_params        )
@order.save
    end

    private
        def order_params
            params.require(:order).permit(:ordertype, :totalprice, :paymentmethod, {orderplaced: {":itemname": ["0"], ":quantity": ["0"], ":unitprice": ["0"], ":tax": ["0"], ":discount": ["0"], ":itemtotalprice": ["0"        ]}})
end

        def orderplaced_params
          order_params[:orderplaced].to_h.map do |order_line_item            |
order_line_item.each_with_object({}) do |(k,v), hsh              |
hsh[k.gsub(":","")] = v["0"            ]
end
          end
        end

        def fixed_order_params
            order_params.slice(:ordertype, :totalprice, :paymentmethod).merge!(orderplaced: orderplaced_params        )
end

    end

Error recieved in orderplaced_params method

NoMethodError (undefined method’ for nil:NilClass):

Pathforward

I wanted to save array of hashes to DB, JSONB format and display them to the receptionaist on Demand.

Can some one help me please.

Advance Thanks…!!! Mukheem.

Hello,

Myself Mukheem. I have just started working with Ruby on Rails.

I am trying to create a small billing application where a receptionist can bill the items and give the receipt to customer.

Now, I wanted to white list all the items from HTML table from front end and save them to DB, JSONB format in a record.

I suspect that you may want to step back and just use a single parameter (text format) for all of your JSON from the page, and just accept it as given by your script. Decomposing the JSON can wait until after you've saved it as a very long string.

White listing a text_field is easy. but I dont know how to do it when the same is present in a HTML table. Some how i managed to reach till this point. Rest of the details are as follows.

There's another (more Rails-y way) to do this, as described here: Ruby on Rails Screencasts - RailsCasts using the `accepts_nested_attributes_for` module and pattern. Your parent object would be, say, a Purchase, which has_many :orders. Using the nested_form gem, you can easily create a form which allows as many items to be added to it as you need, and submits once, yet saves multiple child records, one per order. Your whitelist would be set up like this:

    def project_params       params.require(:project).permit(:name, :user_id, :parent_id, :description, :q,         :jira_number, :box_link, :job_code, :archived, :launch_date, :archive, :producer_id,         items_attributes: [:id, :image, :_destroy, :user_id])     end

That's copied right out of my projects_controller.rb, from an app with the following in it:

    class Project < ApplicationRecord       has_many :items       accepts_nested_attributes_for :items, allow_destroy: true, reject_if: :all_blank       ...     end

In the strong_parameters (whitelist) method, you declare the child object parameters as an array, and when you submit your form on the parent object, the children are taken care of for you in one request.

Walter