http://www.sw.it.aoyama.ac.jp/2019/Projects2/lecture8.html
© 2019 Martin J. Dürst 青山学院大学
Ideal split between controller and view:
@time
or
@count
)SayController
in
app/controllers/say_controller.rb
) withrails generate controller Say hello
(rails generate controller Controller
action
)rails generate model Wrestler name:string height:decimal
...
rails generate model Model
field1:type1 field2:type2
...
)The main datatypes that can be used for model generation are as follows:
Label | Usage/Meaning |
---|---|
:string | (relatively short) character string |
:text | (long) character string |
:binary | (long) binary string |
:date | date (year/month/day) |
:datetime | date and time |
:time | time (of day) only |
:timestamp | date and time, for administrative purposes |
:decimal | decimal (with a fixed number of decimals before and after the decimal point) |
:integer | integer |
:float | float/double |
:boolean | boolean (single bit) |
class Wrestler < ApplicationRecord validates :name, presence: true end
(more at https://guides.rubyonrails.org/active_record_validations.html)
presence: true
numericality: true
numericality: { only_integer: true
}
numericality: { allow_nil:
true }
numericality: { greater_than: 50
}
inclusion: { in: ['序の口',
'序二段', ...] }
(more at https://guides.rubyonrails.org/testing.html#model-testing and other parts of that Web page; see also documentation for Minitest)
test/models/model_test.rb
, e.g.
test/models/wrestler_test.rb
rails test
; it is possible to run only
model tests with rails test:models
test
in a class
ModelTest
(e.g. WrestlerTest
), which is a
subclass of ActiveSupport::TestCase
test
takes a description string and a block
(do
...end
)assert
, assert_equal
, and
many moresetup
method can be used to create some data that can be
reused in some or all of the tests (use instance variables to access the
data in test methods)assert
hoge.
(in
)valid?
), error details
can be checked withassert hoge.errors[:real_name].any?
orassert_equal ["must be greater than 50"],
hoge.errors[:height]
acting
.Actor
with the following fields (in
this order!):name
, real_name
(本名),
birth_date
, origin
, height
,
weight
, gender
(male
or
female
), blood_type
(A
/B
/O
/AB
)Clusivity
. You
can ignore it.rails db:migrate
to generate the databasedb/schema.rb
db/migrate/20190603HHMMSS_create_actors.rb
db/development.sqlite3
(in particular the Structure
tab)id
,
created_at
, and updated_at
, which are generated
and used automatically by Rails)schema.rb
Moodle (deadline: 17:00).actors_controller.rb
,
new.html.erb
, and show.html.erb
(you have to
create a directory actors
in app/view
)resources :actors
http://localhost:3000/actors/new
app/models/actor.rb
to Moodle (deadline:
18:00)rails db:migrate RAILS_ENV=test
to set up the test
databaserails test
. You should get 0 runs, 0 asssertions,
0 failures, 0 errors, 0 skips
.Gemfile.lock
Gemfile
, comment out the lines relating to
capybara
, selenium-webdriver
, and
chromedriver-helper
bundle install
test/models/actor_test.rb
, add tests
for the following. Make sure that for each test, you check that it fails
(when the corresponding validation is commented out) and that it succeeds
(when the corresponding validation is not commented out). Try to use many
calls to test
, with only very few assertions per call.
test/models/actor_test.rb
to Moodle
(deadline: 19:00)db/development.sqlite3
) to Moodle
(deadline Wednesday, 19:00)rails generate model
...
. Try to get an idea about what each line in each file
means. (you do not have to read documentation for this homework, but you
may occasionally have to consult a dictionary)