Creating first demo application using scaffold in Ruby on Rails

Tutorial Title: Creating first demo application using scaffold and rolling back destroying it in Ruby on Rails

Course: Web Engineering

Instructor: Muhammad Samim


Note: Copy and paste commands in terminal after dollar sign ($)


1. Creating folder for web projects.
  • Press Ctrl + Alt +t to open terminal program
  • $ cd
  • $ mkdir webApps
  • cd webApps
2. Creating new project
  • $ rails new scaffoldApp -d postgresql
  • $ cd scaffoldApp
3.  Creating user resource using scaffold
  • $ rails generate scaffold User name:string email:string
4. Creating database is must if working with PostgreSQL using rake command
  • $ rake db:create
5. Migrating database using rake to proceed with application
  • $ rake db:migrate
 6. Rollback database in case if done something wrong with it (Optional)
  • $ rake db:rollback
7. Destroying scaffold
  • $ rails destroy scaffold User
8. Run rails server to connect with local web server
  • $ rails server
9.  Showing created scaffold app on browser (rails server must be running)

10. Click on "New User"

11.Type your name and email then click on "Create User"

12. Request successfully accepted and if you want to edit your information click on "Edit"

  • Click on "Back" and enter some entries
  • After entering some entries
13. Modeling posts
13.1 Generating posts: we use scaffold command for generating posts model
  • $ rails generate scaffold Post content:text user:references
13.2 Migrating Post table to the database
  • $ rake db:migrate
13.3 Validating contents
  • open gedit > file > open > Go to your project folder > app >models and open Post.rb and type the following code.
  • class Post < ActiveRecord::Base
    belongs_to :user
    validates :content, presence: true
    end
13.4 Associating database entities:
  • Press Ctrl + o to Go to open in gedit click on user.rb modify like following code.
  • class User < ActiveRecord::Base
    has_many :posts
    end
13.5 Showing Posts on browser
  • Open terminal
  • $ rails server
  • Open browser and type http://localhost:3000/posts



  • Click on "New Post"
  • Type in content and give ID of User table users
  • Click on "Create Post"
  • Click on "Back"
  • Make some posts for User table's users
  • Back to terminal if server is running press Ctrl +c to stop server
14 Running PostgreSQL to connect and see your database
    • Open terminal
    • $ sudo -u postgres psql
    14.1 Showing your databases
    •  # \l
    •  press q to exit screen of databases to perform next action
    14.2 Connecting to database. (add _development with your database name e.g: FirstApp to FirstApp_development
    • # \c FirstApp_development
    15. Query for showing contents of database
    •  # SELECT * from Users;
    •  press q to exit screen of databases to perform next action
    • # SELECT * from Posts;
    15.1 exiting from database
    • # \q
    16. Working in Rails console.
    • $ rails console
    • > u = User.find(1)
    • > u.posts
    • > u = User.first
    • > u = User.all
    • > u[0]
    • > u[1]
    • > u[2]
    • > u[3]
    • > u[0].posts
    • > u[1].posts
    • > u[2].posts
    • > u[3].posts
    16.1 Creating, Reading, Updating and Deleting operations in rails console

    16.2 Creating: New recored will be created
    • $ rails console
    • > u = User.new
    • > u.name = "Muhammad Samim"
    • > u.email = "muhammadsamim2016@gmail.com"
    • > u.save
    16.2 Reading: Record 1 will be showed
    • > User.find(1)
    16.3 Updating: Record 2 will be modified
    • > u = User.find(2)
    • > u.name = "Herry"
    • > u.email = "herry@email.com"
    16.3 Deleting: reading 4 will be deleted
    • > u = User.find(5)
    • > u.destroy
    SHARE

    Muhammad Samim

    • Image
    • Image
    • Image
    • Image
    • Image
      Blogger Comment
      Facebook Comment

    0 comments:

    Post a Comment