http://www.sw.it.aoyama.ac.jp/2019/Projects2/lecture9.html
© 2019 Martin J. Dürst 青山学院大学
rails generate scaffold wrestler
field1:type1 field2:type2
...rails generate
model command.... scaffold wrestler ... generates:
app/controllers/wrestlers_controlller.rbapp/models/wrestler.rb (class
Wrestler)edit/index/new/show.html.erb)
and one common template (_form.html.erb) in the
app/views/wrestlers directoryScaffolding creates actions that correspond to the database CRUD operations:
| controller action | database operation | SQL operation |
|---|---|---|
| create | create | insert |
| show | read | select |
| update | update | update |
| destroy | delete | delete |
Scaffolding additionally creates the following actions:
index for showing all data of a modelnew to show a form for entering data for a new model
itemedit to show a form to edit existing data| HTTP methods | controller action | database operation |
|---|---|---|
| POST | create | create |
| GET | show | read |
| PUT (also PATCH) | update | update |
| DELETE | destroy | delete |
| GET | POST | |
|---|---|---|
| Purpose | obtain data | send data |
| Data location in request | URI (incl. query part) | URI or body |
| Side effects (changes on the server)? | no (except for logging) | yes (create data in database, buy, pay,...) |
| Idempotent? | yes (same result on repeated access) | no (different result with multiple accessses) |
| PUT | PATCH | POST | |
|---|---|---|---|
| Purpose | replace item at address | replace part of an item at address | accept data at address |
| Response | usually same address | usually newly created address | |
| Use in REST/Rails | update | create | |
resources :wrestlersrails routes.| HTML method | address | controller action |
|---|---|---|
| GET | /wrestlers |
wrestlers#index |
| POST | /wrestlers |
wrestlers#create |
| GET | /wrestlers/new |
wrestlers#new |
| GET | /wrestlers/123/edit
|
wrestlers#edit |
| GET | /wrestlers/123 |
wrestlers#show |
| PATCH | /wrestlers/123 |
wrestlers#update |
| PUT | /wrestlers/123 |
wrestlers#update |
| DELETE | /wrestlers/123 |
wrestlers#destroy |
/wrestlers/123/edit with
GET@wrestler with data for wrestler
123PUTs to
/wrestlers/123update actionupdate action redirects to
/wrestlers/123, using
show.html.erb| XML | JSON | |
|---|---|---|
| Full name | Extensible Markup Language | JavaScript Object Notation |
| Origin | generalization of HTML | extraction from JavaScript |
| Since | 1998 | early 2000s |
| Strengths | document-like structures | close match to programming language data structures |
| Problems | mapping from/to programming language data structures | handling of document-like structures |
| Pieces | elements, attributes | arrays, hashes (called objects), strings, numbers |
| Syntax example | <items>
<item>
<id>1</id>
<title>Buy bread</title>
</item>
</items>
|
[{"id": 1, "title": "Buy bread"}, {...}] |
| Ruby generating library | builder |
jbuilder |
| Ruby parsing library | rexml, nokogiri |
json (JSON.parse) |
Accept header for formatsAccept-Language header for languagebuilder with Railsshow.xml.builder,... in
app/views/...s subdirectoryrequire in view filexml@wrestler (in
show....) or @wrestlers (in
index....)xml.instruct! at the startxml.element_name do ...
endxml.element_name
string_contentitem_url(..., format: :xml)Net::HTTP is such a libraryNet::HTTP.start(host [,
port])request =
Net::HTTP::Get.new('/...')request[header_name] =
header_valuehttp.request(request)response.code,...todo.scaffold for items. Each item has a
title (string), a description
(text), and a due_time
(datetime).rails db:migrate..json versions of some of your items with your
browser..json version of all your items, and
submit it to Moodle as the file 9a_items.json (deadline:
17:00).Gemfile.lock in your application.gem 'builder' to the Gemfile
(preferably just after jbuilder).bundler install.items_controller.rb, add the necessary code for
.xml in parallel to .json.show.xml.builder and
index.xml.builder. Make sure you output all the data (same
data as .json).items_controller.rb,
show.xml.builder, and index.xml.builder to Moodle
(deadline: 17:45).9c_todo_api.rb in a folder
separate from your todo application.id.net/http and json libraries.NOT use a .json extension. Use the request header
Accept: application/json to request data in JSON.JSON.parse to convert the response body to a JSON
object, which can be accesses as a hash or an array of hashes.9c_todo_api.rb to Moodle (deadline:
18:30).items_controller.rb. Be ready to explain each and every single
line in that file.