DEV Community

Brittany
Brittany

Posted on

1

Day 49 : #100DaysofCode - Still Coding Along

Today, I continued to work on my code-along with my bootcamp to duplicate a todo list from this website.

I learned how to create a site controller. A site controller is perfect if you want a main page for your Ruby on Rails project. It is simple to create, first run the following code in your terminal:

rails g controller site index

then add the following code to your config/routes.rb folder.

root 'site#index'
Enter fullscreen mode Exit fullscreen mode

Then edit your app/views/sites/index.html.erb with 'hello world' and visit http://localhost:3000/ in your browser.

You have now set up a site controller for your rails website.

Okay so we have our main page set up, whats next?

The website allows people to be able to create lists. Then they should be able to add items to those lists. They should be able to navigate many lists and see each lists items.

So next step is to create lists that will be able to have items, which meant a model and controller is necessary for lists.

The best generator for that is the resource generator.

rails generate resource list name

That generated the following:

invoke active_record create db/migrate/20200718235127_create_lists.rb create app/models/list.rb invoke test_unit create test/models/list_test.rb create test/fixtures/lists.yml invoke controller create app/controllers/lists_controller.rb invoke erb create app/views/lists invoke test_unit create test/controllers/lists_controller_test.rb invoke helper create app/helpers/lists_helper.rb invoke test_unit invoke assets invoke scss create app/assets/stylesheets/lists.scss invoke resource_route route resources :lists
Enter fullscreen mode Exit fullscreen mode

In addition, it created the following List table in the db folder:

class CreateLists < ActiveRecord::Migration[6.0]
  def change
    create_table :lists do |t|
      t.string :name

      t.timestamps
    end
  end
end
Enter fullscreen mode Exit fullscreen mode

The table looks correct so next run, rails db:migrate and then check run the console to make sure the database can be edited properly.

Try this:

rails c
List.create(:name => "FirstList")
List.all

Great now we can update the config/routes.rb so that the main page will be able to see lists. Add the following code to the routes.rb

resources :lists

Now update your app/controllers/lists_controller.rb with the following method

def index
 @lists = List.all
end

Enter fullscreen mode Exit fullscreen mode

and update your app/views/lists/index.html.erb

<ul>
<% @lists.each do |list| %>
   <li>
    <div class="view">
     <label> <%= link_to list.name, list_path(list) %> </label>
     </div>
    </li>
    <% end %>
</ul>
Enter fullscreen mode Exit fullscreen mode

Now visit http://localhost:3000/lists and you should be able to see all the lists.

Tomorrow I will give the steps for creating items for the lists. :)

Thanks for reading!

Sincerely,
Brittany

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

đź‘‹ Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay