Ask A Question

Notifications

You’re not receiving notifications from this thread.

jbuilder serializer for returning INDEX endpoint not working like it did with me.json.jbuilder ....

Megazoor asked in General

Hello! I finished your rails api - react-native demo and am begging for more react-native rails demonstrations... I am building a game with rails/react now using your tutorial as my base layer for using the api and setting up the env.

I have added an index var to the users class and am returning the proper json in /api/v1/index.json but when i try to add my serialzer its not recognizing it... i am still in the user class and have the route ad everything setup... wondering what im doing wrong?? Heres the code::

# users_controller.rb

 def index
     @index = render json: User.all, adapter: :json
  end 
#   routes.rb

  namespace :api do
    namespace :v1 do
      get '/me' => "users#me"
      get '/index' => "users#index"
    end
  end
# /views/api/v1/users/index.json.jbuilder

json.array! @index do |user|
  json.id user.id
  json.email user.email
  json.name user.name
  json.avatar_url gravatar_image_url(user.email, size: 40)
end
Reply

Hey Joel!

I think it's because of your render method. When you render json: it's going to call the to_json method on what you pass in, not render the template.

Change that to the following and you should be fine. Basically we just want to do the query in the controller and give the variable a better name. Then Rails will know to render index.json.jbuilder when the request comes in with .json at the end.

def index
  @users = User.all
end

And accordingly, change your index.json.jbuilder file to use the @users variable to match what was set in the action.

json.array! @users do |user|
Reply

that works now thanks!!

I am wondering... if i wanted to pass the Signup data from the app directly to the rails api to signup or sign in without redirecting to the webview how would i go about that?
<3

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,464+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.