How to Use the Resource Generator to Create Posts
In this lesson, we are going to create a Post model using the resource generator. We are using the resource generator as it will create all the files we need for our model, unlike a a scaffold that would create a ton of files, some of which we may not even need.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

In this lesson, we are going to create a Post model using the resource generator. We are using the resource generator as it will create all the files we need for our model, unlike a a scaffold that would create a ton of files, some of which we may not even need. This would lead to code bloat in our project, which would lead to a number of issues.

To use a resource generator, run the following command in the terminal:

rails g resource Post date:date rationale:text

large

As you can see, this command has created our models, controllers, helpers, assets and view files. However, it doesn't fill the files with code like the scaffold generator does.

Next, let's run rake db:migrate to update the database.

A great aspect of the resource generator is it creates a number of files in the spec folder. You can view them to see what they look like. Now, let's run rspec to verify that everything is working fine, and it should be fine.

Next, open the post_spec.rb file and make some changes to it like this.

# spec/models/post_spec.rb

require 'rails_helper'

RSpec.describe Post, type: :model do
  describe "Creation" do
    it 'can be created' do
      post = Post.create(date: Date.today, rationale: "Anything")
      expect(post).to be_valid
    end
  end
end

In this test we're checking to see if the create functionality is working for posts. As you probably notice, it is similar to what we did for users in the previous lesson.

Let's run the tests to see if this is working.

large

As you'll notice this is working and the tests are passing.

However, in the real world sense, it has a number of issues. Open your console in sandbox mode and try to create a post like this:

Post.create()

This would work, but then it'll have no values inside. This will cause problems for us in the future, so we cannot allow objects with nil values. To fix this, go back to post_spec.rb and create a new test called "cannot be created without a date and rationale". Inside this code block, we'll do the same thing that we did with user. The complete code should look like this:

# spec/models/post_spec.rb

require 'rails_helper'

RSpec.describe Post, type: :model do
  describe "Creation" do
    before do
      @post = Post.create(date: Date.today, rationale: "Anything")
    end

    it 'can be created' do  
      expect(@post).to be_valid
    end

    it 'cannot be created without a date and rationale' do
      @post.date = nil
      @post.rationale = nil
      expect(@post).to_not be_valid
    end
  end
end

If you run the same test again, the output will be one failure and one success because our Post object does not have a date and a rationale.

To fix this problem, open post.rb and include this code:

# app/models/post.rb

validates_presence_of :date, :rationale

If you go back to the console and run the tests again, both will pass.

Before we end, let's see what the resource generator has created for us. If you go to views folder, you'll see an empty subfolder called posts. It also created stylesheets and javascripts for posts , and both these are located inside assets folder. Also, it created a post_controller for us. If you open routes.rb, you can see that it created a resource for posts.

In the next guide, we'll start building out the real functionality.

Resources