Last time we moved our functions into a Relationship class and we're ready to begin testing out Rails for the first time. However, in order to serve up a web page, we need to write up a simple controller and view so that we can pull the data from our Relationship class (in models) to the view so that the user can use it.
So, first off, I needed to make a couple of quick alterations to the code we finished with last time. Apparently I left a typo in there and it turns out that I do not need to explicitly include the radiator gem in that file. But what I did need to do was include it in the Gemfile which holds all of the dependencies and run a bundle install command to download the needed dependencies into my project.
Next, we need to create a view and a controller so that we can set up a simple server locally to view that our functions are indeed working correctly. Luckily enough, Rails has ways to generate some basic code and put the necessary files in the right location. I ended up running the following command in order to generate the needed folders:
Through this command, I've generated a controller which is named content_controller.rb and a view in a folder named Content called index.html.erb. Erb is embedded ruby which allows us to write Ruby within HTML code. Everything is placed in the correct location and Rails is even nice enough to generate some sample code for you! Below shows the output of the command:
Now we can start writing some code. But before we do, I'll quickly explain the whole architecture. The Relationship class contains our relationships which will be called by the controller. Inside the controller we'll have a function named index. The index function basically collects the data from the model using the functions from the Relationship class and then passes that data to the view named index while also rendering that view which Rails does automatically behind the scenes. The code for this whole process is relatively simple:
In the controller above, we basically have those two calls to the different relationships. We then assign the return values from each of those function calls to two different instance variables. The instance variable here is shared with the rest of the controller class and this allows us to pass them to the view when the view is rendered.
Our HTML view is relatively simple. We simply have a header and we use the embedded ruby syntax to grab our permlinks from the instance variables we stored earlier. The final view can then be seen by starting a local server by using the command rails s within the project. We then need to type in the server and port the application is running on as well as the file path required for the view.
And boom! Just like that we have developed a basic web application using the Rails framework. Granted it has taken awhile to get to this point, one can imagine, with a little practice, that through Rails we can generate and develop web applications quickly and effectively while also providing some structure to allows for future scalability. Next time, we'll play around with some routing and then get started on our next relationship.