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 Browser's Request

gene c
gene c
13,630 Points

How does the server know about the /posts extension?

we didnt create the posts page or type anything into the command line to generate the post page.

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

RoR has a lot of cool "magic" built-in. When Jay created the "post" Model, he used the command (see below)

The command produces the extensive scaffold code (shown below)

treehouse:~/workspace/blog$ bin/rails generate scaffold Post title:string  

Running via Spring preloader in process 21126                                             
      invoke  active_record                                                               
      create    db/migrate/20191216195338_create_posts.rb                                 
      create    app/models/post.rb                                                        
      invoke    test_unit                                                                 
      create      test/models/post_test.rb                                                
      create      test/fixtures/posts.yml                                                 
      invoke  resource_route                                                              
       route    resources :posts                                                          
      invoke  scaffold_controller                                                         
      create    app/controllers/posts_controller.rb                                       
      invoke    erb                                           
      create      app/views/posts                                                         
      create      app/views/posts/index.html.erb                                          
      create      app/views/posts/edit.html.erb                                           
      create      app/views/posts/show.html.erb                                           
      create      app/views/posts/new.html.erb                                            
      create      app/views/posts/_form.html.erb                                          
      invoke    test_unit                                                                 
      create      test/controllers/posts_controller_test.rb                               
      create      test/system/posts_test.rb                                               
      invoke    helper                                                                    
      create      app/helpers/posts_helper.rb                                             
      invoke      test_unit                                                               
      invoke    jbuilder                                                                  
      create      app/views/posts/index.json.jbuilder                                     
      create      app/views/posts/show.json.jbuilder                                      
      create      app/views/posts/_post.json.jbuilder                                     
      invoke  assets                                                                      
      invoke    coffee                                                                    
      create      app/assets/javascripts/posts.coffee                                     
      invoke    scss                                                                      
      create      app/assets/stylesheets/posts.scss                                       
      invoke  scss                                                                        
      create    app/assets/stylesheets/scaffolds.scss  

It created all the MVC (Model View Controller) code which makes it possible to do CRUD (Create Read Update Delete) on the model through the web framework.

Check out this video where he generates the scaffold.

https://teamtreehouse.com/library/our-first-resource

After you complete the scaffold, you have to "migrate" the database before it will work (see below)

treehouse:~/workspace/blog$ bin/rails db:migrate RAILS_ENV=development

== 20191216195338 CreatePosts: migrating ======================================           
-- create_table(:posts)                                                                   
   -> 0.0209s                                                                             
== 20191216195338 CreatePosts: migrated (0.0210s) ============================= 

And then, finally, run the server:

treehouse:~/workspace/blog$ bin/rails server -b 0.0.0.0                                   
=> Booting Puma                                                                           
=> Rails 5.2.4 application starting in development                                        
=> Run `rails server -h` for more startup options                                         
Puma starting in single mode...                                                           
* Version 3.12.2 (ruby 2.6.3-p62), codename: Llamas in Pajamas                            
* Min threads: 5, max threads: 5                                                          
* Environment: development                                                                
* Listening on tcp://0.0.0.0:3000                                                         
Use Ctrl-C to stop