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
- $ rails new scaffoldApp -d postgresql
- $ cd scaffoldApp
- $ rails generate scaffold User name:string email:string
- $ rake db:create
- $ rake db:migrate
- $ rake db:rollback
- $ rails destroy scaffold User
- $ rails server
10. Click on "New User"
| Some links with their description | ||
| URL | Action | Purpose |
| http://localhost:3000/users | index | page to list all users |
| http://localhost:3000/users/1 | show | page to show user with id 1 |
| http://localhost:3000/users/new | new | page to make a new user |
| http://localhost:3000/users/1/edit | edit | page to edit user with id2 |
- Click on "Back" and enter some entries
- After entering some entries
13.1 Generating posts: we use scaffold command for generating posts model
- $ rails generate scaffold Post content:text user:references
- $ rake db:migrate
- 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
- 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
- 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
- Open terminal
- $ sudo -u postgres psql
- # \l
- press q to exit screen of databases to perform next action
- # \c FirstApp_development
- # SELECT * from Users;
- press q to exit screen of databases to perform next action
- # SELECT * from Posts;
- # \q
- $ 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.2 Creating: New recored will be created
- $ rails console
- > u = User.new
- > u.name = "Muhammad Samim"
- > u.email = "muhammadsamim2016@gmail.com"
- > u.save
- > User.find(1)
- > u = User.find(2)
- > u.name = "Herry"
- > u.email = "herry@email.com"
- > u = User.find(5)
- > u.destroy


0 comments:
Post a Comment