loading...

Day 29 : #100DaysofCode - Very Simple Rails App CRUD Practice

sincerelybrittany profile image Brittany ・4 min read

#100DaysofCode (65 Part Series)

1) Day 1 of #100DaysofCode - Understanding Iterators 2) Day 2: #100DaysofCode - I updated to Catalina and All HELL Broke Loose 3 ... 63 3) Day 3: #100DaysofCode - Oh No, SQL 4) Day 4: #100DaysofCode - More Iterators 5) Day 5 - #100DaysofCode - Setting up a Sinatra App 6) Day 6 : #100DaysofCode - Setting up a Sinatra App : Part 2 - Config.ru 7) Day 7: #100DaysofCode - Setting up a Sinatra App - Part 3 8) Day 8: #100DaysofCode - Setting up Sinatra Database - Part 4 9) Day 9: #100DaysofCode - ActiveRecord and a Database 10) Day 10: #100DaysofCode - ActiveRecord and a Database 11) Day 11 : #100DaysofCode - RESTful Routes 12) Day 12: #100DaysofCode and Day 1 of #Javascript30 13) Day 13 - #100DaysofCode - #Javascript30 - Day 2 - CSS + JS Clock 14) Day 14: #100DaysofCode - Finalized my Sinatra Project -Security 15) #Day 15: #100DaysofCode - View my Sinatra Project 16) Day 16 - #100DaysofCode - Understanding MVC 17) Day 17 : #100DaysofCode - Knowing your Ruby Version & what Errno::EADDRINUSE means is important! 18) Day 18 of #100daysofCode - 3 challenges 19) Day 19 of #100daysofCode - Hashes 101 20) Day 20: #100DaysofCode - Practice makes perfect 21) Day 21 : #100DaysofCode - Cascading Style Sheets 101 22) Day 22 - #100daysofCode - CSS 101 Part 2 23) Day 23 - #100DaysofCode - Updated My Portfolio 24) Day 24 - #100DaysofCode - Practicing CSS Grid and Emmet Shortcuts on VSCode 25) Day 25 - #100DaysofCode - Intro to Rails 26) Day 26 - #100DaysofCode - Accessing Rails Commands 27) Day 27 : #100DaysofCode - Still reviewing the basic rails concepts . . . 28) Day 28 : #100DaysofCode - Adding Dev.to blogs to personal page 29) Day 29 : #100DaysofCode - Very Simple Rails App CRUD Practice 30) Day 30 : #100DaysofCode - Very basic rails continued 31) Day 31 - #100DaysofCode - Rails Routes 101 32) Day 32 : #100DaysofCode - Resources for learning to code 33) Day 33 - #100DaysofCode - Setting up my app on Heroku 34) Day 34 - #100DaysofCode - New/edit action versus create/update action 35) Day 35 : #100DaysofCode - A Code Challenge Completed 36) Day 36 - #100DaysofCode - Rails form_for versus form_tag 37) Day 37 : #100DaysofCode - 30 seconds of code 38) Day 38 - #100DaysofCode - Built my first basic rails application 39) Day 39 : #100DaysofCode - I updated my github profile page 40) Day 40 : #100DaysofCode - I need project ideas 41) Day 41 : #100DaysofCode - Collaboration 42) Day 42 : #100DaysofCode - The Planning of a Project 43) Day 43 : #100DaysofCode - The Amazing Faker Gem 44) Day 44 : #100DaysofCode - Code or Youtube? 45) Day 45 : #100DaysofCode - Basic Nested Forms 46) Day 46 : #100DaysofCode - Still reviewing form_for 47) Day 47 : #100DaysofCode - How do you code? 48) Day 48 : #100DaysofCode - Code Along 49) Day 49 : #100DaysofCode - Still Coding Along 50) Day 50 : #100DaysofCode - Keep Coding Along 51) Day 51 : #100DaysofCode - Code Along 52) Day 52 : #100DaysofCode - No Wifi, No Problem 53) Day 53: #100DaysofCode - Created user log in and log out functionality 54) Day 54 : #100DaysofCode -Review on setting up log in & sign up 55) Day 55 : #100DaysofCode - In Need of Advice on Reading Poorly Written Documentation 56) Day 56 : #100DaysofCode - A Tweet - Devise - Users 57) Day 57 : 100DaysofCode - Remembering to Git Commit Often 58) Day 58 : #100DaysofCode - Beginning to Learn Python 59) Day 59 : #100DaysofCode - Focused on My Rails Project 60) Day 60 : #100DaysofCode - CSS in Rails 61) Day 61 : #100DaysofCode - Omniauth? 62) Day 62 : #100DaysofCode - Sign in using a Third Party, Rails 63) Day 63 : #100DaysofCode - Refactoring 64) Day 64 : #100DaysofCode - Still Playing Around With OmniAuth 65) Day 65 : #100DaysofCode - Second Rails Project - I Want to Try Self-Referencing Tables

Today I focused on a rails simple CRUD code-along. I watched a youtube video and went step-by-step on how to create an application that can display an item name and item quantity and allow a user to conduct CRUD functionality on it.

So what was the first step? The database of course!

Database/Generators

Within the database, I wanted to create an Items table with a name column that should be a string and a quantity column that should be an integer. Something like this:


Table name: Items
-- name : string
-- quantity : integer

Rails is amazing because I no longer have to manually set up a table, rails allows users to use generators to get things done.

For example, if all I wanted to do was create the db table/ migration there is a migration generator. With the following:

rails g migration

However, no one wants JUST the migration because you will still need to make your models, controller, and views.

So instead I decided to use the models generator. The models generator will create a migration and model for us. So, I ran the following:

rails g model item name:string quantity:integer

The above will automatically generate an item model and migration.

When you go to app/models/item.rb you will see this:

class Item < ApplicationRecord
end

It also generated your migration within your database. You will see that a table was automatically generated within db/migrate/create_items.rb and will look similar to this:

class CreateItems < ActiveRecord::Migration[6.0]
  def change
    create_table :items do |t|
      t.string :name
      t.integer :quantity
      t.timestamps
    end
  end
end

You will also see that it generated test_unit files that will come in handy in the future for creating test.

After generating your model and migration, it is best to migrate your database and mess around with it to make sure it works. Go ahead and run the following,

rails db:migrate or rake db:migrate

then jump into console and play around:

rails console

Within your console, try creating an Item.

 i = Item.create(name: "glasses", quantity: 5)

If you run i or i.all you should see the item that you just created.

GREAT! Now what? Now, we create our controller.

And guess what! Rails has a controller generator.

rails g controller items

Note that the controller name that you choose should be pluralized. The controller generator will create a controller, views folder, test unit files, assets files, and helper files.

If you want to specify what methods should be included in your controller and the view files that you want to be generated included in your file, run the following:

rails g controller items index show

After running that, if you look at your views you will see pre-written methods within app/controllers/items_controller.rb, like so:

class ItemsController < ApplicationController

  def index

  end

  def show

  end

end

So how do you see all of the items within your database? And where would that restful route be? When you think about it, you do not want to go to localhost3000/index to view all of your items. It makes more sense to go to localhost3000/items

So how do we do that?

You need to take the pre-defined method in your items controller called index and just like Sinatra, grab all the items from your database and present them in your view.

Add the following to your index method, within your app/controllers/items_controller.rb.

class ItemsController < ApplicationController

  def index
     @items = Item.all
     render :index #the render is optional
  end

  def show

  end

end

Note, that I put that adding render is optional because rails implicitly knows to look for a directory with the same controller name and a file with the same action name that will automatically render your view. So, technically, you no longer have to render, you can leave it blank because rails is magic (just kidding-- there no such thing as magic, said no one).

However, since I am new to rails, I chose to specify what view my index method will render.

render :index

Since we used the controller method generator to generate our views, we do not need to create a index view, but if it is not there/you chose not to use the generator, simply create a file named index.html.erb in your app/views/item folder by running: touch app/views/items/index.html.erb

Within your index view, add the following code so that you can display all of your items on the index view:

<ul>
  <% @items.each do |item| %>
 <li> <%= item %> </li>
  <% end %>
</ul>

Last, the major difference between rails and sinatra is you have to specify your routes. To add a route you need to go to the routes folder, config/routes.rb

Create a route by adding the following:

get '/items', to: 'items#index', as: items

The code above, pretty much handles the get '/items' request of /items,
then it sends the get '/items' request to the index method in the items controller. Then it specifies the path as:items so that it is easily referenced in the future when using the rails link_to method.

Now when you run rails server and head over to localhost/3000/items in your browser you should see the glasses - 5 that you originally created in your rails console.

#100DaysofCode (65 Part Series)

1) Day 1 of #100DaysofCode - Understanding Iterators 2) Day 2: #100DaysofCode - I updated to Catalina and All HELL Broke Loose 3 ... 63 3) Day 3: #100DaysofCode - Oh No, SQL 4) Day 4: #100DaysofCode - More Iterators 5) Day 5 - #100DaysofCode - Setting up a Sinatra App 6) Day 6 : #100DaysofCode - Setting up a Sinatra App : Part 2 - Config.ru 7) Day 7: #100DaysofCode - Setting up a Sinatra App - Part 3 8) Day 8: #100DaysofCode - Setting up Sinatra Database - Part 4 9) Day 9: #100DaysofCode - ActiveRecord and a Database 10) Day 10: #100DaysofCode - ActiveRecord and a Database 11) Day 11 : #100DaysofCode - RESTful Routes 12) Day 12: #100DaysofCode and Day 1 of #Javascript30 13) Day 13 - #100DaysofCode - #Javascript30 - Day 2 - CSS + JS Clock 14) Day 14: #100DaysofCode - Finalized my Sinatra Project -Security 15) #Day 15: #100DaysofCode - View my Sinatra Project 16) Day 16 - #100DaysofCode - Understanding MVC 17) Day 17 : #100DaysofCode - Knowing your Ruby Version & what Errno::EADDRINUSE means is important! 18) Day 18 of #100daysofCode - 3 challenges 19) Day 19 of #100daysofCode - Hashes 101 20) Day 20: #100DaysofCode - Practice makes perfect 21) Day 21 : #100DaysofCode - Cascading Style Sheets 101 22) Day 22 - #100daysofCode - CSS 101 Part 2 23) Day 23 - #100DaysofCode - Updated My Portfolio 24) Day 24 - #100DaysofCode - Practicing CSS Grid and Emmet Shortcuts on VSCode 25) Day 25 - #100DaysofCode - Intro to Rails 26) Day 26 - #100DaysofCode - Accessing Rails Commands 27) Day 27 : #100DaysofCode - Still reviewing the basic rails concepts . . . 28) Day 28 : #100DaysofCode - Adding Dev.to blogs to personal page 29) Day 29 : #100DaysofCode - Very Simple Rails App CRUD Practice 30) Day 30 : #100DaysofCode - Very basic rails continued 31) Day 31 - #100DaysofCode - Rails Routes 101 32) Day 32 : #100DaysofCode - Resources for learning to code 33) Day 33 - #100DaysofCode - Setting up my app on Heroku 34) Day 34 - #100DaysofCode - New/edit action versus create/update action 35) Day 35 : #100DaysofCode - A Code Challenge Completed 36) Day 36 - #100DaysofCode - Rails form_for versus form_tag 37) Day 37 : #100DaysofCode - 30 seconds of code 38) Day 38 - #100DaysofCode - Built my first basic rails application 39) Day 39 : #100DaysofCode - I updated my github profile page 40) Day 40 : #100DaysofCode - I need project ideas 41) Day 41 : #100DaysofCode - Collaboration 42) Day 42 : #100DaysofCode - The Planning of a Project 43) Day 43 : #100DaysofCode - The Amazing Faker Gem 44) Day 44 : #100DaysofCode - Code or Youtube? 45) Day 45 : #100DaysofCode - Basic Nested Forms 46) Day 46 : #100DaysofCode - Still reviewing form_for 47) Day 47 : #100DaysofCode - How do you code? 48) Day 48 : #100DaysofCode - Code Along 49) Day 49 : #100DaysofCode - Still Coding Along 50) Day 50 : #100DaysofCode - Keep Coding Along 51) Day 51 : #100DaysofCode - Code Along 52) Day 52 : #100DaysofCode - No Wifi, No Problem 53) Day 53: #100DaysofCode - Created user log in and log out functionality 54) Day 54 : #100DaysofCode -Review on setting up log in & sign up 55) Day 55 : #100DaysofCode - In Need of Advice on Reading Poorly Written Documentation 56) Day 56 : #100DaysofCode - A Tweet - Devise - Users 57) Day 57 : 100DaysofCode - Remembering to Git Commit Often 58) Day 58 : #100DaysofCode - Beginning to Learn Python 59) Day 59 : #100DaysofCode - Focused on My Rails Project 60) Day 60 : #100DaysofCode - CSS in Rails 61) Day 61 : #100DaysofCode - Omniauth? 62) Day 62 : #100DaysofCode - Sign in using a Third Party, Rails 63) Day 63 : #100DaysofCode - Refactoring 64) Day 64 : #100DaysofCode - Still Playing Around With OmniAuth 65) Day 65 : #100DaysofCode - Second Rails Project - I Want to Try Self-Referencing Tables

Posted on by:

sincerelybrittany profile

Brittany

@sincerelybrittany

Web Developer | Software Engineer 👩🏾‍💻 | Determined | Music & Dance | #100DaysofCode | #WomenWhoCode

Discussion

markdown guide