Continuing with the blog example from part 1 and 2, I am going to add a feature that lets visitors to post a message and save it to the DB.
Creating Message model and controller
Before creating the model, I am going to set up the Rails application to use MySQL locally.
Add the mysql2 gem to
Gemfile and run
#Gemfile #... gem mysql2 #...
database.yml content to the following:
#database.yml development: adapter: mysql2 database: blogapp_development username: root pool: 5 timeout: 5000
Now, create the message model along with its controller by running the following under the
rails generate model message name:string content:string rails generate controller messages rake db:create rake db:migrate
At this point, You should be able to go to MySQL and see the
blogapp_development database and the
In order for us to test our new feature, we need to create a form that allows users to submit messages and a page where users can view others’ messages. Update the following files:
<%= form_for @message do |f| %> Name: <%= f.text_field :name %><br/> Content: <%= f.text_field :content %><br/> <%= f.submit %> <% end %>
<% @messages.each do |message| %> Author: <%= message.name %> <br/> <%= message.content %><br/><br/> <% end %> <%= link_to "New Message", new_message_path %>
class MessagesController < ApplicationController def index @messages = Message.all end def new @message = Message.new end def create Message.create(message_params) redirect_to messages_path end private def message_params params.require(:message).permit(:name, :content) end end
Blogapp::Application.routes.draw do root "messages#index" resources :messages end
Start your Rails app locally, go to
localhost:3000/messages/new and make sure that you can post a message and view it on
Creating MySQL RDS instance in AWS
Log in to your AWS account and click on the RDS link. In there, you are going to find a big blue button labeled Launch DB Instance. Click it.
Step 1. You are going to choose the database engine. Select MySQL Community Edition.
Step 2. Is this DB going to be used for production? Well, yes, but since this is only for demonstration purposes, we are going to select No so that we can use the free usage tier.
Step 3. You are going to configure the username, password, instance size etc. Take note of your username and password because you are going to need it later. Feel free to allocate more storage for the instance, but know that it will cost you more.
Step 4. More configuration you have to set, but really the important one for us right now is just going to be the DB name. In this step, you will also see configuration for the DB Security Group(s). Leave this to default for now. We are going to revisit this later.
Step 5. This is where you can configure automatic backups. Feel free to disable this but I am just going to leave it as-is
Step 6. Review and launch the instance!
In step 4, we left the DB instance to use the default security group. We want to set this security group so that we can use the
mysql command to access the DB console.
If you are not already on the RDS Dashboard, go there and click on the Security Groups link on the side navigation bar.
You will see the list for RDS security groups here, but you will most likely only see one which is the default. Click on the magnifying glass right next to default to show its details.
Add a new rule to allow access to your DB from anywhere. Enter 0.0.0.0/0 for CIDR, click Add and Refresh Security Groups. It will take a 1-2 minutes for the security group’s Status to change to Authorized.
Once you’re done with all of the above, you can access your instance using the
mysql command line. Like so:
mysql -h yourins.mydnsnameexample.rds.amazonaws.com -P 3306 -u admin -p #You will be prompted for your password
Configure Rails to use the new RDS instance
We are done for now with the RDS Dashboard.. Now, go back to the blogapp Rails project.
config/database.yml, and modify it appropriately with your DB instance’s name, username, password and host.
development: adapter: mysql2 database: blogapp_development username: root pool: 5 timeout: 5000 production: adapter: mysql2 database: blogapp username: admin host: yourins.mydnsnameexample.rds.amazonaws.com password: blogappadmin port: 3306 pool: 5 timeout: 5000
Commit all of your changes and push it.
git add . git commit -m "Form for message submission and production RDS setting" git push origin master
Let’s go over what we just did. We created a new RDS instance and allow it to be accessible by the outside world. We also set up our Rails project to use this RDS instance in production environment. Next, we are going to update our app in EC2 to have the latest code and run migration. If everything goes smoothly, we should then be able to start our Rails app in the cloud and post messages just like we did locally.
Pull code, migrate and run in production
Log in to your EC2 instance. If your forgot how, here is the SSH command. Change the location of the .pem file and the public DNS to yours.
ssh -2 -i ~/location/to/your/blogapp.pem firstname.lastname@example.org
Go to the blogapp app folder and
git pull origin master to update the code.
Run the following to update gems and run migration
bundle install rbenv rehash rake db:migrate RAILS_ENV=production
You should see that the messages table that we created earlier is now created in the MySQL RDS instance. The last step is to restart the Passenger server
rbenv sudo passenger stop -p80 rbenv sudo passenger start -p80 -d -e production
Open up a browser and enter the EC2 instance address (Public DNS). You should see the updated page with the New Message link. Try creating a new message and see it show up on the messages index page.
That’s it! You now know how to manually set up a server to host your Rails application. I hope that was not too bad. Again, send me an e-mail if you have any questions.
More: assets pre-compilation
rbenv sudo rake assets:precompile RAILS_ENV=production