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

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

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|

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

Join the discussion

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

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

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

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.