This might be a terribly simple question, but this is just something I noticed that is bothering me.
I'm trying to render JSON from one of my controller's methods, but it's giving me a "undefined method `new' for nil:NilClass" error.
Here's the code that's causing the problem:
def index
@users = User.all
render json: @users
end
I noticed that when I try to render only one object to JSON, everything works fine:
def show
@user = User.find(params[:id])
render json: @user
end
Or when I call to_json on the @users object:
def index
@users = User.all
render json: @users.to_json
end
I was under the impression that calling render json: was implicitly calling to_json anyway, so why would calling that twice solve my issue?
undefined method newstack trace?render json:wouldn't automatically take the array.