http://www.sw.it.aoyama.ac.jp/2019/Projects2/lecture11.html
© 2019 Martin J. Dürst 青山学院大学
belongs_to :movie
; movie has_many
:reviews
)review.movie.title
or
@review.movie.title
Wrong Steps:
rails generate migration
...
)rails db:migrate
)Correct Steps:
rails generate migration
...
)rails db:migrate
)Important: NEVER edit a migration that is already executed!
Two alternatives
rails db:migrate:status
)rails db:migrate:status
))rails db:rollback STEP=1
for a
single migration)rails db:migrate
)params
: URI parametersparams[:password]
session
: Session object that stays between requests.session[:user_id]
: Keeps the user_id of the user who is
logged in.flash
: Used for errors and alertsflash[:alert]
review.user.name
)nil
, which leads to a
NoMethod
errornil
without calling a
method if the receiver is nil
&.
review&.user&.name
nil
is allowed and sometimes expected.link_to
is a command to create links.link_to "Logout", logout_url, method: :delete
Function | Form for Model Object | Independent Form |
Command | form_with |
form_tag |
Block variable | form | - |
Label | label | label_tag |
Submission | submit | submit_tag |
If a field is declared as type digest
, then Rails helps as
follows:
password_digest
to the database. This is a
cryptographic hash of the actual password.has_secure_password
command to the model.password
and
password_confirmation
to new
/edit
forms (in _forms
partial).authenticate
method to check the password.However, Rails does not automatically provide the following:
cinema
applicationcinema
application (and you have already done last week's
exercises 10a/10b/10c), start with exercise 10a, then proceed to this
week's exercisescinema
application next
weekUser
model for users, with passwords.bcrypt
gem: Delete the file
Gemfile.lock
. In the file Gemfile
, uncomment the
line for the bcrypt
gem. Run bundle install
.cinema
application, create a scaffold
for users. Each user
has a userid
(string
), a name
(string
), an
email
(string
), and a password
(digest
).changeme
). Do NOT use a password that you use
in any real Web application.11a_users_controller.rb
to Moodle
(deadline 17:00).AddUserRefToReview
.user
) and the type (references
), separated
by a colon (:
).column:type
with the scaffold
command.add_reference
in the
change
method. has_many
and belongs_to
commands to the respective models._form.html.erb
.user_id
parameter.index
and
show
). This is very similar to exercise 10b.11bShow.html.erb
and
11bIndex.html.erb
(deadline: 17:30).Sessions
controller is not a full REST controller, so we
use rails generate controller
to create it.generate
command, use the controller
name (Sessions
) and the actions new
,
create
, and destroy
.create
action:
params[:parameter_name]
to access URI
parameters.userid
with
User.find_by
.authenticate
method to check
the password.session[:user_id]
to user.id
. Redirect to movies_url
using the
redirect_to
command.login_url
, with an alert:
indicating that the
password (or the userid) is wrong.new
view:
h1
).form_with...
to form_tag
form
block parameteruserid
and password
.label
to label_tag
,
xxxx_field
to
xxxx_field_tag
.submit
to submit_tag
and add the
text "Login" as a parameter.controller :sessions do get 'login' => :new post 'login' => :create delete 'logout' => :destroy end
app/layouts/application.html.erb
), before the
yield
command, add an if
statement that shows the
login status (logged in as user name, or not logged in).if
statement, add a link (with link_to) to login
(if logged out) or logout (if logged in).11cSessionsController.rb
,
the view file for the new action as 11cNew.html.erb
, and the
application layout file as 11cApplication.html.erb
(Deadline:
18:30).(no need to submit)