1720034973

Learn Ruby on Rails lesson 2


# Anatomy of a rails project. This gives a pretty general overview. Most of the time you will spend exist inside the app dir/. At the beginning you will change your configs and routes and stuff but once your settled in you'll make most changes from app/. Most things that you will need to do within other dir/'s can be done from console anyways with rails cmds. Im not going to go into much debth on all the parts of the project. You can find all of them here: <table> <thead> <tr> <th>File/Folder</th> <th>Purpose</th> </tr> </thead> <tbody> <tr> <td>app/</td> <td>Contains the controllers, models, views, helpers, mailers, channels, jobs, and assets for your application. You'll focus on this folder for the remainder of this guide.</td> </tr> <tr> <td>bin/</td> <td>Contains the <code>rails</code> script that starts your app and can contain other scripts you use to set up, update, deploy, or run your application.</td> </tr> <tr> <td>config/</td> <td>Contains configuration for your application's routes, database, and more. This is covered in more detail in Configuring Rails Applications.</td> </tr> <tr> <td>config.ru</td> <td>Rack configuration for Rack-based servers used to start the application. For more information about Rack, see the Rack website.</td> </tr> <tr> <td>db/</td> <td>Contains your current database schema, as well as the database migrations.</td> </tr> <tr> <td>Gemfile<br>Gemfile.lock</td> <td>These files allow you to specify what gem dependencies are needed for your Rails application. These files are used by the Bundler gem. For more information about Bundler, see the <a href="https://bundler.io">Bundler website</a>.</td> </tr> <tr> <td>lib/</td> <td>Extended modules for your application.</td> </tr> <tr> <td>log/</td> <td>Application log files.</td> </tr> <tr> <td>public/</td> <td>Contains static files and compiled assets. When your app is running, this directory will be exposed as-is.</td> </tr> <tr> <td>Rakefile</td> <td>This file locates and loads tasks that can be run from the command line. The task definitions are defined throughout the components of Rails. Rather than changing <code>Rakefile</code>, you should add your own tasks by adding files to the <code>lib/tasks</code> directory of your application.</td> </tr> <tr> <td>README.md</td> <td>This is a brief instruction manual for your application. You should edit this file to tell others what your application does, how to set it up, and so on.</td> </tr> <tr> <td>storage/</td> <td>Active Storage files for Disk Service. This is covered in Active Storage Overview.</td> </tr> <tr> <td>test/</td> <td>Unit tests, fixtures, and other test apparatus. These are covered in Testing Rails Applications.</td> </tr> <tr> <td>tmp/</td> <td>Temporary files (like cache and pid files).</td> </tr> <tr> <td>vendor/</td> <td>A place for all third-party code. In a typical Rails application this includes vendored gems.</td> </tr> <tr> <td>.gitattributes</td> <td>This file defines metadata for specific paths in a git repository. This metadata can be used by git and other tools to enhance their behavior. See the <a href="https://git-scm.com/docs/gitattributes">gitattributes documentation</a> for more information.</td> </tr> <tr> <td>.gitignore</td> <td>This file tells git which files (or patterns) it should ignore. See <a href="https://help.github.com/articles/ignoring-files">GitHub - Ignoring files</a> for more information about ignoring files.</td> </tr> <tr> <td>.ruby-version</td> <td>This file contains the default Ruby version.</td> </tr> </tbody> </table> <hr> This table is grabbed off the [rubyonrails.org](https://rubyonrails.org/). Its a great resource to learn from. I recommend their active records lessons as its very simple yet still shows you most of the useful features. Anyways. Getting back to the main parts of the application that you will be interacting with. App/. cd into app/ and lets take a look at the contents. The ones that matter the most to you will be models controllers and views. These 3 work together. Models will create a template of sorts. This template is used to model (hence the name) information that will get passed to the view. The view is what takes the model and based on the logic within the controllers will render and output for the user. The controller is the logic bits that will interface with the back end. It is possible to skip the models and only use views and controllers but thats not good practice. Now since we intend to have a webpage with more then 1 page we will need to look a the routing capabilities we have. Start your rails server back up with ```rails server``` then navigate to your local host. In your URL append /up. You should see a green background and nothing else. This means everything is working as it should. There are actually quite a few routes here that are setup for you to begin with. Your root url (just a /) and /up are the ones you will mostly check for status/errors aswell as your normal output. If you want to add your own pages to your application you will need to set up a route connected to a controller to permit that action. As we are intending to build a multi-page application we will do that next. cd.. back to the main rails dir/. Change into the config dir/. Near the end of the files (assuming your are organized alphabetically) you will find the routes.rb file. open it up and take a look. It should be pretty empty with the exception of some comments. Leave it as for now. I mentioned in lesson 1 that a rails project can be made without generators. You could make your own controller in the app/ then have a route point to it. Rails is intended to get rid of all that time wasting hassle and get us straight to the design and logic bits by handling all the file creation and config. It would be rude of us to ignore all their effort. So lets use their generator! *note. there are still times when you will need to make changes to routes so its good to know where to find it. Lets add a route to our routes.rb ```get "/post", to: "post#index"``` This page is going to have post that users make so that others can view them. Just like [chat-to.dev's post page](https://chat-to.dev/new). Now that we have a way to navigate to that page give it a try. Dont worry it wasnt supposed to work. We havent used the generator to handle it for us yet. Stop your server with ```CTRL + C```. ```rails generate controller Post index --skip-routes``` Tada generation complete. Give it a try now. Not much should be there but it shouldn't throw an error any more. The reason that we appended --skip-routes is because we manually made it above. Mostly just to show you the syntax for making a route. If you would like to make another but have the generator handle the routes, feel free to generate again. Just be sure to make it something other then Post so you dont get conflicts. Other then the routes that you have now made we also have a few new files to look at. Head back to app/. Check your views and controller. You should see some new files in there named by what you generated. Congrats. You now have pages for your site. Empty ones. But pages none the less. We will fill them up next lesson.

(3) Comments
amargo85
amargo85
1720037608

Edit the post. something's wrong with it. the characters have left the main div

xReqX
xReqX
1720038175

yea the table dont allow sideways scrolling or line wrapping i guess. ill dump it as its not really useful info. list can be found by typing a incorrect url in anyways as a dummy page will showup letting you know the other routes you can pick.

amargo85
amargo85
1720038331

good! that way you don't take away from the style of your post