Ruby Ruby on Rails 5 Basics Handling Requests The Browser's Request

gene c
gene c
13,629 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
Jeff Muday
Treehouse Moderator 23,731 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/                                     
      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.

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                                   
=> 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://                                                         
Use Ctrl-C to stop