DEV Community

Discussion on: Create a Basic API with Ruby on Rails

Collapse
cescquintero profile image
Francisco Quintero 🇨🇴 • Edited on

Hey, nice tuto. It's impressive how simple is to build a REST API with Ruby on Rails. Love it.

I also think you hit a good point by mentioning a standard way to output JSON when scaling or the API is getting big. JSON output is one of those things that no one agrees how to return them 😅

Suggestion: do not use instance variable in API controllers. In Rails (IMO), they make more sense when working with views.

# Do this
def index
  bands = Band.all
end

# instead of 
def index
  @bands = Band.all
end
Enter fullscreen mode Exit fullscreen mode
Collapse
decentralizuj profile image
decentralizuj • Edited on

Nice comment... Instance variables make sense in controllers, so could be used in views:

@band    = Band.find_by(id: params[:id])
@guitars = Guitar.where(band_id: @band.id).all
Enter fullscreen mode Exit fullscreen mode

Now we can see band's guitar on band page.

In API, this is done by Serializers.