【问题标题】:Handle regular expression in ruby在 ruby​​ 中处理正则表达式
【发布时间】:2012-04-14 20:05:43
【问题描述】:

在我的控制器中,我从表中获取一些记录。我正在尝试将记录记录发送到 Js 并显示在我的页面中。

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

@item_list 的输出为

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

向JS端发送记录

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

在Js端

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

警报结果是这样的

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

任何人都可以建议我如何在我的视图页面中显示。

我知道如何在下面解析并显示在视图页面中。

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

可以像这样转换我的@item_list 值或让我知道其他建议

【问题讨论】:

  • 我完全看不懂题主。无论如何,发送 JSON 而不是对象数组的字符串表示不是更有意义吗?
  • 尝试使用 to_json WebView.execute_js("replaceItemList('#{@item_list.to_json}')")
  • @Joshua:如果任何字段中有单引号,这将失败,因此.gsub("'", "\\'") 也是必要的。

标签: javascript jquery ruby rhomobile


【解决方案1】:

我的要求是将表(数据库记录)发送到前端并显示。 所以我喜欢下面 默认传输
@item_list = TransferDetail.find(:all) 首选附件内容 = 获取首选附件内容 WebView.execute_js("replaceItemDetails('#{preferred_accessories_content}')") 结束

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

这里“preferred_accessories_content”的结果是正常的html表格行,所以我可以通过JS将此行附加到我的视图页面。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 2011-09-14
    • 1970-01-01
    相关资源
    最近更新 更多