Ruby on rails https://youtu.be/7r7UUulUdag

Ruby on rails https://youtu.be/7r7UUulUdag,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我想将id转换为字符串,以便在url中使用 @tutorial_id = Demotutorial.where("job_id = ?", @job_id).select( "id") @t_id = @tutorial_id.to_s render json: @t_id 获取此错误 s SyntaxError:JSON.parse:JSON数据的第1行第1列出现意外字符 Json数组教程 {"demotutorials":[

我想将id转换为字符串,以便在url中使用

@tutorial_id = Demotutorial.where("job_id = ?", @job_id).select( "id")
@t_id = @tutorial_id.to_s
   render json: @t_id
获取此错误 s SyntaxError:JSON.parse:JSON数据的第1行第1列出现意外字符

Json数组教程

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

有两件事可以解决你的问题

如果使用结果为activerecord关系的位置,则可以 有两条记录,你必须选择我给你的那一行 先采样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