Simple Twist


Collapse Content

There are already many existing blog platforms, and you can find open-source Rails ones as well. Why build your own platform? Besides building one to learn Rails, it makes sense to build your own platform if you have some new idea or twist that you'd like to implement.

Let's customize the blog platform so it's different than the other ones out there. In the style of Thing Explainer, we'll only allow posts written with ~1000 of the most common words.

Words

Let's first create a way to check if a Word in a post is allowed. We can do this by creating a Word model and then check if a each word in a post is in the words table. Go ahead and create a Word model for this purpose.




By now, you should know how to do this:

rails generate model Word word:text

I named both the model and column word since that seemed like the best word to use. If you find that confusing, you can change the model name to "Entry".

Before running the migration, can you add an index to the migration file to make looking up words fast?

Migration Code

After modifying the file, run rake db:migrate to create the Words table. Test out your Word model by adding a few words to it in the console. For example:

Word.create!(word: "hello")
Word.create!(word: "the")
Word.create!(word: "the")

The first two lines should create new SQL entries, while the third one should thrown an error.

validate

To prevent posts from being saved with unapproved words, let's add a validation to the Post model. There's no built-in validation for this purpose, but Rails let's you create custom validations. The simplest way is to use the method validate and pass it your own method written for this purpose. Add the following line to your Post model:

  validate :no_big_words

Now go ahead and create that method at the end of your Post model:

class Post < ActiveRecord::Base

 #....

  private

   def no_big_words
   end

end

Next, fill in the method. Here's one way to do it:

def no_big_words
  big_words = Word.invalid_words(content)  # 1
  if big_words.present?
    errors.add(:content, "Not found: #{big_words.join(' ')}.")  #2
  end
end
  1. This calls the method invalid_word which we'll soon create.
  2. If there are any invalid words, it adds an error to the the errors collection. To convert the array to a string, we use the .join method.

Now we just need to create the invalid_words method. Below is one way to do that.

class Word < ActiveRecord::Base

  def self.invalid_words(content)
    return if content.blank?
    return content.downcase.gsub(/[^A-Za-z \n]/, '').split.reject do |word|
      find_by(word: word)
    end
  end

end

(Hover above for more info.)

This code uses a regular expression (or "regex") to detect non-letters, which it removes by replacing them with ''. Regular expressions are useful for manipulating text, but they can be confusing. You can read a tutorial on them [here](http://ruby.bastardsbook.com/chapters/regexes/ or try out your own regex at Rubular.

After removing non-letters, the code splits the text into words and rejects any word that we find in the database. It then returns this collection of invalid words. This will be displayed in the error message on the user's form.

Contact Us
Sign in or email us at [email protected]