ruby - Rendering JSON for multiple Rails objects -
this might terribly simple question, noticed bothering me.
i'm trying render json 1 of controller's methods, it's giving me "undefined method `new' nil:nilclass" error.
here's code that's causing problem:
def index @users = user.all render json: @users end
i noticed when try render 1 object json, works fine:
def show @user = user.find(params[:id]) render json: @user end
or when call to_json
on @users object:
def index @users = user.all render json: @users.to_json end
i under impression calling render json:
implicitly calling to_json
anyway, why calling twice solve issue?
i believe it's issue @users
being array of objects needs each object converted first before whole array reassembled , output json.
Comments
Post a Comment