我目前正在将ActiveModelSeralizer切换到JBuilder来渲染jsons.我想知道,使用ActiveModelSeralizer我可以做类似的事情:
text_content = UserSeralizer.new(user,:root => false)@H_403_4@并在名为text_content的变量中接收json字符串.既然我正在转离ActiveModelSeralizer,那么无论如何都要使用JBuilder进行上述操作吗? @H_403_4@我有app / view / api / v1 / users / _user.json.jbuilder里面的部分视图是否有将部分渲染为变量? @H_403_4@谢谢
解决方法
是的你可以.只需使用Jbuilder.encode方法:
# somewhere in User model def display_to_json Jbuilder.encode do |json| json.name name json.location display_location json.description description json.work_experience work_experience end end@H_403_4@并使用它:
<!-- somewhere in view,just for example --> <div ng-init="user = <%= @user.display_to_json %>"></div>@H_403_4@注意:类名是Jbuilder,而不是JBuilder.