Starting a fresh project with Ruby on Rails
As I started to grow accustomed to using Ruby on Rails, it becomes easier to move around the framework and get things to work the way I want them to. The more I use Rails the more it becomes clear how I need to set things up. In this tutorial I will talk about the commands necessary to get a basic app started.
With RoR install it is very easy to start a new application. Type the command:
rails new <Name of App> — database=postgresql
You can replace the database argument with whatever database you want to use but postgresql seems to be the defacto DBMS when it comes to creating Rails apps.
After you finish this, you may need to go into the database.yml file to change some of the database credentials so that your application has access to the database.
Now that our app is connected with the DBMS we can create our database for the application. Run the following command to do so
rake db:create
Once that is complete we will need to generate our controller and view. First fun the following command to create the controller
rails generate controller <Name of Controller>
Then we need to add this to our routes table. Add the following to the file route.rb located in our config folder.
root ‘<Name of Controller>’#index
This is a simple method for hooking up just the index page and there are other ways to hook to multiple routes when we need them.
Next we need to add the index action to the controller file we generated. An action is simply a function definition nested with in the controller class. When someone visits our page, the URL is checked against the routes table and the corresponding action in the corresponding controller is used. This means anything in the action will be triggered before the view generated. This could be anything from retrieving data from the database or redirecting the user to another page.
Once the action is created we can simply create a view with text. The name of the view file must correspond with the action and be located in the folder that is the same as the controller. Once the view is created you should be able to access that page from your browser.
Lastly, we will create a model to go with our app. Use the following command to generate a model
rails generate model <Name of Model>
A migration file will have been created in the db folder. This is what we edit to tells rails how to modify the table. Add the columns you will use in your table then use the following command to execute the table changes.
rake db:migrate
If you made a mistake with the migration you can rollback the last change with the command
rake db:rollback
Now you will have access to the model from the controllers. To allow access to data from the database in your view you need to add a line such as
@mode_name = Model.all
To you controller. Then you can access the contents from the view and display them on your page.
This is a simple guide to get you off your feet when creating a RoR application. There is a lot more that can be done and will need to be done if you are to create something more than a static page but this will be the jumping off point before the difficult stuff comes into play. Hope this guide helps!