https://youtu.be/7r7UUulUdag

https://youtu.be/7r7UUulUdag

我想将 id 转换为字符串以在 url

中使用
@tutorial_id = Demotutorial.where("job_id = ?", @job_id).select( "id")
@t_id = @tutorial_id.to_s
   render json: @t_id

出现这个错误 小号 语法错误:JSON.parse:JSON 数据

的第 1 行第 1 列出现意外字符

教程Json数组

{"demotutorials":[{"demotutorials":{"id":50}}]}

你的问题有两点

  • 如果你使用结果是activerecord关系的地方,它可以 有几条记录,你必须 select 哪一行,我给你了 sample first fow 选择第一条记录
  • @tuturial_id 是一个activerecord行,所以你必须选择哪一列,我给你的示例使用@tutorial_id.id

下面是示例代码:

@tutorial_id = Demotutorial.where("job_id = ?", @job_id).select( "id").first
@t_id = @tutorial_id.id.to_s
render json: @t_id

以下代码适合我

@tutorial_id = Demotutorial.where("job_id = ?", @job_id).select( "id").first
@t_id = @tutorial_id[0].id.to_s
render json: @t_id