Sinatra is a Ruby framework for creating simple web applications with minimal effort. in this post I will walk you through my Applicant Tester list app. If you’re interested in the code, you can find it in this Github repository . You are welcome to make any contributions.
Structure the MVC
The most challenging task for any beginner in Sinatra (like me) is to structure the Model Views Controller correctly. Specifically, the process of setting up the environment. A helpful tool that can ease the structuring process of the Model Views Controller is a cool gem called corneal — this is the link to the repo. Corneal is designed and created by Brian Emory and is a Ruby gem with the purpose of generating Sinatra apps. You only need to install the gem by running gem install corneal
and run corneal new <app name>
in the terminal. Then, run bundle install to install all missing gems, that’s it! You may refer to the full instructions in the corneal README file for more information.
Building the ActiveRecord associations
ActiveRecord has built-in macros for setting up object relationships. All you need to do is find the appropriate macros for your project. This application has two relationships: the company object has_many applicants and the applicant object belongs_to a company.
Uses a password for authentication
Encryption is crucial to protect a companies data. ActiveRecord’s has_secure_password method accomplishes this perfectly. To utilize this macro, I took these steps:
- The application must have bcrypt-ruby gem installed.
- The companies create_table must have a string column for “password_digest”.
Then, you can use has_secure_password macro.
Start running your database(db)
In the db/migrate directory, you’ll see the companies_table
and applicant_table
. You have to create the table and identify the type of data. Then, in the terminal, run rake db:migrate
to migrate your tables and obtain the schema file.
Testing the pages
It is a rule of thumb to start carefully and test each step you did. Otherwise, you will find yourself in a series of unpredictable errors. I started with the application controller, confirming that the index page works using get.
Applying CRUD and using RESTful routes
CRUD means Create, Read, Update and Delete. “… ,a RESTful route provides mapping between HTTP verbs, controller actions, and (implicitly) CRUD operations in a database.” (Railsguide)
Companies controller
The companies controller was responsible for implementing the company sign-in/out/up, and to edit the company profile. The authenticate method was used to confirm the password with the one being tracked by the password_digest column in the db.
Sign-up/sign-in forms
These simple HTML form tags allow us to use two main attributes: the action attribute, which I assigned to /signup route; and the method attribute, which is set to “POST.” Sign-in forms are easy, but can be tedious.
The ‘show.html.erb’ company profile page
To show the company his profile page, you will need to pull out company.name from the company object. This is achieved by using the ERB syntax — iterating over the company object and set the dynamic route to print out the company name of the current company using the company.id route.
Edit the company profile information
To edit, you have to use the PATCH HTTP verb, but it won’t work directly! The browser doesn’t support the POST and DELETE verbs. To overcome this issue, you have to make the following two steps:
- Use the Rack::MethodOverride inside your config.ru file. Allow the method to be overridden when the
params[:_method]
is set. This is the “middleman” that supports the PATCH/PUT and DELETE HTTP methods. - Establish an input tag with the following attributes: id and type set to hidden, name to a “_method” and finally the value to “PATCH”.
and that’s it, the company can edit their profile.
Read a specific companies Applicant list
The second most challenging task (behind setting up a perfect environment) is to guarantee that the company is the only one permitted to view/edit/delete their related applicants. To do so, I used the binding.pry and checked out @applicants.methods to grab the targeted one. The QueryMethod that I chose was named “where.” It’s job is to search for a saved company ID in the db that matches the current company ID and display the corresponding Applicants list.
Validating your entry fields
You can block the company from bad entries, such as bad email formatting or leaving one of the fields empty. To do so, you need to use ActiveRecord
validations for the entry fields. In the Company class model, I used the validates
shortcut format to authenticate the entry. In the Models folder of the Company class, I created a constant for email format using RegEx.
Conclusion
ActiveRecord is basically magic, but to be a real modern day wizard, much more knowledge is required. To know how to use ActiveRecord, you need to parse through innumerable documents… Hence, you would learn a huge number of methods that you can add to your wizards tome.
Acknowledging contributing articles
- Sinatra-portfolio-project Github repository
- Corneal gem this is the link to the repo
- Github ToDo list repo for Salma71
- DB browser for sqlite.
- RESTful guide table — Railsguide
- Rack::MethodOverride documentation rubyonrails
- ‘Where’ query-method documentation ActiveRecord
- Validation methods ActiveRecord:Validation