3. Prerequisite
What is Ruby?
A programming language
What is Rails?
A web application framework programmed by Ruby
4. Prerequisite
MVC
Model-View-Controller
Refer to this slide for other details
http://www.slideshare.net/jaceju/mvc-8761203
Img src: http://www.ils.unc.edu/~uskala/MVC.htm
5. Prerequisite
REST
CRUD REST Note
Create Post New data
Read Get Read data
Update Put Update data
Delete Delete Delete data
6. Prerequisite
Installation
Windows base
http://railsinstaller.org/
Trouble shooting
ERROR: Error installing json: The 'json' native gem requires
installed build tools.
http://stackoverflow.com/questions/8100891/the-json-
native-gem-requires-installed-build-tools
7. Prerequisite
Create a new project
rails new your_project_name
Start the created project
cd project_name
bundle install
rails server
Check http://localhost:3000 or http://127.0.0.1:3000
8. Prerequisite
Project organization
/app
Organize your models, views, and controllers
/config
Configs such as database config (database.yml), rails
environmental config (environment.rb)…etc
/db
Manage your database
9. Prerequisite
Project organization
/doc
Docs generated by RubyDoc framework
/lib
Libs for your applications
/log
Error log such as “server.log”, “development.log”…etc
/public
Some html files for your applications
10. Prerequisite
Project organization
/script
Some scripts for your applications
/test
Some tests written by you or by rails such as unit
test, fixture tests…etc.
/tmp
Temp files
/vendor
Third party libs
12. First App
We can use the following command to new a
controller and related files
rails generate GENERATOR ARG
Generator can be assets, controller, helper…etc
ARG here is the file name
14. First App
Testing your http://127.0.0.1:3000/greeting
Edit your config/routes.rb
resources :greeting
http://stackoverflow.com/questions/11696695/routing-error-ruby-on-rails
15. First App
After updating the routes.rb
Add an index function in your controller
def index
render :text=>”Hello Ruby world!”
end
16. First App
Aforementioned approach is somehow dirty
We need to follow the MVC rules
Update the index function in controller
def index
@welcome_msg = "Hello RoR world“
end
Update view/index.html/erb
<%= @welcome_msg %>
17. First App
Worth to mention what erb file is
ERB file = html code + ruby code
Ruby code will be surrounded by <%...%> or <%=…%>
<% code here %> : pure logic, nothing display to screen
<%= code here %> : display something to screen
18. First App
Worth to mention the Request scope
Request scope
Each http request will get a copy of the controller
Pros : thread safe
Cons: hard to share data between different requests
Solution: use sessions