Rails link_to调用的多个数据集和类型

时间:2019-03-08 06:55:02

标签: ruby-on-rails

尽管documentation for link_to明确说明了如何处理一组自定义数据属性,但以下URL具有多个数据调用,包括数据属性的数据子集。我怀疑由于data选项的逻辑循环,到目前为止的尝试都失败了。

https://domain.me/chart?c={
  type:'bar',
  data:{
    labels:['01','02','03','04', '05'], 
    datasets:[
      {label:'A',data:[1,2,3,4,5]},
      {label:'B',data:[10,11,12,13,14]}
    ]
  }
}

如何构成link_to rails调用以正确生成此语法?

1 个答案:

答案 0 :(得分:0)

怎么样:

"https://domain.me/chart?c=#{{
  type:'bar',
  data:{
    labels:['01','02','03','04', '05'], 
    datasets:[
      {label:'A',data:[1,2,3,4,5]},
      {label:'B',data:[10,11,12,13,14]}
    ]
  }
}.to_query}"

哪个生成:

https://domain.me/chart?c=data%5Bdatasets%5D%5B%5D%5Blabel%5D=A&data%5Bdatasets%5D%5B%5D%5Bdata%5D%5B%5D=1&data%5Bdatasets%5D%5B%5D%5Bdata%5D%5B%5D=2&data%5Bdatasets%5D%5B%5D%5Bdata%5D%5B%5D=3&data%5Bdatasets%5D%5B%5D%5Bdata%5D%5B%5D=4&data%5Bdatasets%5D%5B%5D%5Bdata%5D%5B%5D=5&data%5Bdatasets%5D%5B%5D%5Blabel%5D=B&data%5Bdatasets%5D%5B%5D%5Bdata%5D%5B%5D=10&data%5Bdatasets%5D%5B%5D%5Bdata%5D%5B%5D=11&data%5Bdatasets%5D%5B%5D%5Bdata%5D%5B%5D=12&data%5Bdatasets%5D%5B%5D%5Bdata%5D%5B%5D=13&data%5Bdatasets%5D%5B%5D%5Bdata%5D%5B%5D=14&data%5Blabels%5D%5B%5D=01&data%5Blabels%5D%5B%5D=02&data%5Blabels%5D%5B%5D=03&data%5Blabels%5D%5B%5D=04&data%5Blabels%5D%5B%5D=05&type=bar

您可能需要摆弄一点。