In our app postit, the vote function is applied to both Post model and Comment model. A post has many votes and a comment also has many votes. In this case, we will use polymorphic associations.
First we will add votes table, rails generate migration create_votes.
In general, we can use authentication gems like Devise and OmniAuth to build up the login and logout process. But here, we will start from scratch and implement it in our postit.
First we will add has_secure_password to the user model. In the user.rb file,
classSessionsController<ApplicationControllerdefnewenddefcreateuser=User.find_by(username:params[username])ifuser&&user.authenticate(params[:password])session[:user_id]=user.idflash[:notice]="You've logged in!"redirect_toroot_pathelseflash[:error]="There were something wrong with your username or password."render:newendenddefdestroysession[:user_id]=nilflash[:notice]="You've logged out!"redirect_toroot_pathendend
Following the actions in sessions#controller, we will need new.html.erb.
Now the login and logout structure is ready to use and in order to make the life easier to modify the view templates, we need several helper methods.
1234567891011121314
defcurrent_user@current_user||=User.find_by(sessions[:user_id])ifsession[:user_id]enddeflogged_in?!!current_userenddefrequire_userunlesslogged_in?flash[:error]="This action is not allowed."redirect_toroot_pathendend
VoilĂ ! Next step will be to customize the view templates to logged_in user and not.
First, add has_secure_password method to the User model which will allow us to save the password through “one-way hash”, meaning the hash of the password strings will each turn into long, undecipherable tockens.
Second, add a new column password_digest to the users table to save the password token. The password string should never be stored in the application and they can only be digested. If you register at a site and they send you your password in plain text, do not trust the site.
Then make sure gem bcrypt-ruby installed.
After the three steps, virtual attributes password and password_confirmation and authenticate() method can be used in our app.
When user.authenticate(password) is true, it will return the user object. Otherwise, it will return false.
After we run rake db:migrate in the command line, it will create six columns in the database,
primary key: id
url
title
description
created_at
updated_at
Then in the app/models folder, we will create the corresponding model,
12
classPost<ActiveRecord::Baseend
The file name will be in singular form, post.rb.
Tips on table name: use the tableize method on the class name. For example, 'Post'.tableize => “posts”.
If we have a Comment model and there is a 1:M association between Post and Comment, a foreign_key column - post_id need to be added to the comments table and it will point to the primary key column in the posts table. Then in the Post and Comment model file, we will do the following changes.
If there is a Category model and it has a M:M association with Post, a joint model and table will be needed to build up the association. The model file name will be post_category.rb and class name will be PostCategory. To get the table name, 'PostCategory'.tableize and the output is ‘post_categories’. There will be two foreign key columns in the table, post_id and category_id. In the model file, we will set up the has_many :through.
As mentioned in my previous blog, I just built up a simple online Blackjack game based on Sinatra. Here is the link and you can play with it.
Sinatra is a DSL to build up web applications quickly in Ruby with minimal efforts. It’s such a great tool that I want to write a post-it note.
Include the gem require 'sinatra' and then gem install sinatra
Just deployed the Blackjack game to heroku and this marks the end of TeaLeaf Academy first course - Introduction to Ruby and Web Development.
I started the program on Oct 26th, 2014 and it took me exactly one month to complete the course. I wouldn’t say it’s a hard course, but absolutely not easy. It has been such a great experience for me that I would highly recommend.
For future reference, I will list what I have done and learnt here.
Today is Nov 23rd, 2014 and I just finished Lesson 3 on TeaLeaf Academy.
It has been three weeks since I joined the program and I was surprised by the progress I made. In three weeks, I built up three games in two different approach, procedural and OO. Then game Blackjack was upgraded from the command line to the web by using Sinatra. And today I have my own blog based on Octopress. This is awesome!
Before joining the program, I have been going through different tutorials online but in the end, I’m still wondering where I should start and lacking confidence in building up web applications. After doing a lot of research and visiting bootcamps in Toronto, I chose TeaLeaf because it’s online, cheaper and I don’t need to quit my full time job. Its alumni also gave very positive feedback.
Why am I doing this? To make more money. Yes, that’s why I started. But as I get more and more knowledge and practice, it becomes more and more interesting for me. I love it! Two hours a day from Monday to Friday and over 10 hours in the weekend were spent on it. I’m addicted to it, like chocolate.
I know it’s a long process and requires patience and commitment to become really good at it. But it’s never too late to start. I would like to record this experience and write down the tips I learnt and mistakes I made.