DEV Community

Brittany
Brittany

Posted on

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

Top comments (0)