Sign in

My Rails Porject

Before it gets started

This is my third project of Flatiron bootcamp. After two months hard working I gained some understanding of web development. Rails is very similar to Sinatra only more integrated and effective. In Sinatra if we want to implement some features we may have to add some gems, with Rails some of them are already included. Let’s dive in!

Coming up with an idea

The gist of using these frameworks to develop a website is the same, what’s different is how you want to relate the associations between models in order for them to implement the fuctionality that you wish users would see and interact with.

Models, attributes, associations and validations

Apparently this project will have a user model, a trip model and a flight model. User has many flights through trips and flight has many users through trips too. Solid as a rock.

Controllers actions, routes and view pages

After setting up the models, we need to design our controller and routes flow. I want a home page, all user page and flight page to be independant. And I also want my trip pages to be nested under other routes. Just like the real life, you would only want to book a trip after confirm the flight you’d like to take. So trip’s new and create actions will be nested under fligths. When you check your trip, it’s more likely to be a property of a user, which makes other trip pages nested within users.

Authentication module and third party login service

This is a very handy feature that you could easily implement by just adding omniauth, omniauth-specific-third party-service and dotenv-rails gems to your gemfile.In my case I used facebook which makes omniauth-specific-third party-service omniauth-facebook. What we need to do next to set up this login service is to go to the service and set up project key and secret, and add it in a .env file in the root directory of your project. Be sure to protect it by adding it to .gitignore, otherwise everyone could have the access. Last we need to create a omniauth.rb in our config/initializers with the code below

RESTful routes

From here it’s just what we do all month. Create a new object for form builder in that page to handle the form parameters and authenticity token. Use strong params to protect from forging form information and redirect to corresponding page if the object can be sucessfully saved, otherwise we need to handle errors and tell the user which part of the form they didn’t fill up right. For show and index page we need to be aware of what kind of data we’re rendering to the viewer, so we’d better prepare the right instance variables beforehand.

Helper method

After spending some time working on our actions, we’ll definately find out many of our code is repetitive. This is where our helper method comes in. For my project I defined a #current_user, a #home_page_if_logged_in and a #require_logged_in to abstract same code that limits actions. Also I have set up #set_obj or #set_obj_and_others to reduce same code repetition by using a before_action helper method.

Partial and Layout

My partials are almost as many as my view pages.

Last

It’s always stressful and fun to start a project from nothing. Thank you for reading. See you next time!