1

In my controller i m taking some records from table. I am trying to send records records to Js and show in my page.

 @item_list = TransferDetail.find(:all) - Code to get data from table

The output of @item_list as

[{:source_id=>8, :object=>"11375810_0", :prefix=>"a",:unit=>"0", :description=>"xxxxx"}, {:source_id=>8, :object=>"11375810_1", :prefix=>"b", :unit=>"0", :description=>"yyyyy"}]

Sending records to JS side

WebView.execute_js("replaceItemList('#{@item_list}')") 

In Js side

function replaceItemList(item_list){
  alert (item_list);
 }

Alert result as like this

[{:source_id=>8, :object=>"11375810_0", :prefix=>"a",:unit=>"0", :description=>"xxxxx"}, {:source_id=>8, :object=>"11375810_1", :prefix=>"b", :unit=>"0", :description=>"yyyyy"}]

Any one can suggest me to how can i show in my view page.

I know how to parse below and show in view page.

"{\"transferType\":\"D\", \"accountNumber\":\"132\", \"employeeId\":\"23\", \"orderedByPhone\":\"2423453453\", \"deliveryInstructions\":\"fdgfghvbn\"}"

Is possible to convert my @item_list value like this or Let me know some other suggestion

vinothini
  • 2,606
  • 4
  • 27
  • 42
  • I don't understand the question title at all. In any case, wouldn't it make more sense to send JSON rather than a string representation of an array of objects? – Dave Newton Apr 14 '12 at 20:12
  • Try using to_json `WebView.execute_js("replaceItemList('#{@item_list.to_json}')")` – Joshua Cheek Apr 14 '12 at 20:26
  • @Joshua: This will fail if there's a single quote in any of the fields, so `.gsub("'", "\\'")` is necessary as well. – Niklas B. Apr 14 '12 at 22:59

1 Answers1

0

My requirement is send table (database records) to front end and to show. So i did like below def transfer
@item_list = TransferDetail.find(:all) preferred_accessories_content = get_preferred_accessories_content WebView.execute_js("replaceItemDetails('#{preferred_accessories_content}')") end

def get_preferred_accessories_content
    preferred_accessories_content = ""
    @item_list.each do |pa|
      preferred_accessories_content+= "<tr>" + "<td> #{pa.description} </td>"+"</tr>"
    end
    preferred_accessories_content
end

Here the result of "preferred_accessories_content" is normal html table row, so i can append this row to my view page through JS.

vinothini
  • 2,606
  • 4
  • 27
  • 42