Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby on Rails 5 Basics Handling Requests The Controller

Oğulcan Girginc
Oğulcan Girginc
24,848 Points

What is the meaning of GET /posts.json comment?

In the PostsController every action has 2 comments:

# GET /posts
# GET /posts.json
def index
  ...
end

# GET /posts/1
# GET /posts/1.json
def show
  ...
end

# GET /posts/new
def new
  ...
end

# GET /posts/1/edit
def edit
  ...
end

# POST /posts
# POST /posts.json
def create
  ...
end

# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
  ...
end

# DELETE /posts/1
# DELETE /posts/1.json
def destroy
  ...
end

The first ones are pretty self-explanatory to me. However, I couldn't understand why the scaffold generates comments that include a .json version of HTTP requests?

1 Answer

Marco van Vemden
Marco van Vemden
12,553 Points

Hi Ogulcan,

Scaffolding is mostly an educational tool to provide you with a somewhat dynamic example, and to demonstrate Rails best practices. If you have Jbuilder in your Gem files (included in Rails 4 by default), the scaffolding command will generate the files as a way to have a template for responding to a JSON query.

If you want to view the output, fire up your (local) server and visit: /posts.json