Save 36% for Black Friday! Learn more

Ask A Question

Notifications

You’re not receiving notifications from this thread.

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

Abdul Mukheem Shaik asked in Rails

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. :(

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.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,110+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.