AAAA
<% if session[:username] %> <form method="post" action="/users/signout"> <p class="user-status"> Signed in as <%= session[:username] %>. <button type="submit">Sign Out</button> </p> </form> <% else %> <p class="user-status"><a href="/users/signin">Sign In</a></p> <% end %>
Explanaion:
1. if in the session hash, key :username has a value, then perform this:
- create a form , the form will create a HTTP post request to /user/signout
- the form will only submit when the button is clicked, that's why it has a type="submit"
- otherwise, if the button is not clicked, just display the paragraph/message of "Signed in as username"
2. Without username value
- create a link to '/users/signin' page with a HTTP GET request, with a text "Sign In"
BBBB (Views/Sigin)
<form method="post" action="/users/signin"> <div> <label for="username"> Username: <input name="username" value="<%= params[:username] %>"/> </label> </div> <div> <label for="password"> Password: <input type="password" name="password" /> </label> </div> <button type="submit">Sign In</button> </form>
Explanaion:
1. When redirected to the sign in , there is a form with a HTTP post request to "/users/signin"
2. There is two division created by the two <div>
3. First <label> is for username , there is an input tag that create a params[:username].
The value attribute will display username if there is one already.
CCCC
get "/users/signin" do erb :signin end
Explanaion:
When redirected to the sign in via GET , This will render view/sigin erb page.
post "/users/signin" do if params[:username] == "admin" && params[:password] == "secret" session[:username] = params[:username] session[:message] = "Welcome!" redirect "/" else session[:message] = "Invalid credentials" status 422 erb :signin end end
Explanaion:
When directed to the "/users/signin" page via a HTTP post request, then check if params[:username]
is equal to "admin" and params[:secret] is equal to "secret". If yes, then set username variable to
session[:username]. Also create a key/value hash with the value "Welcome!".
If the username is not "admin" or password is not "secret", or both, then create a key/value has with
the value "Invalid credentials". Assigned a status of 422
Status code 422 :
1. understands the content type of the request entity
2. syntax of the request entity is correct
3. was unable to process the contained instructions
Then render the erb page "signin" again, because it is invalid credential
post "/users/signout" do session.delete(:username) session[:message] = "You have been signed out." redirect "/" end
Explanaion:
In the case of HTTP POST to "users/signout" , then you delete the :username session. Append the
message "You have been signed out."
Redirect to "/".
<% if session[:username] %> <form method="post" action="/users/signout"> <p class="user-status"> Signed in as <%= session[:username] %>. <button type="submit">Sign Out</button> </p> </form> <% else %> <p class="user-status"><a href="/users/signin">Sign In</a></p> <% end %>
Explanaion:
1. if in the session hash, key :username has a value, then perform this:
- create a form , the form will create a HTTP post request to /user/signout
- the form will only submit when the button is clicked, that's why it has a type="submit"
- otherwise, if the button is not clicked, just display the paragraph/message of "Signed in as username"
2. Without username value
- create a link to '/users/signin' page with a HTTP GET request, with a text "Sign In"
BBBB (Views/Sigin)
<form method="post" action="/users/signin"> <div> <label for="username"> Username: <input name="username" value="<%= params[:username] %>"/> </label> </div> <div> <label for="password"> Password: <input type="password" name="password" /> </label> </div> <button type="submit">Sign In</button> </form>
Explanaion:
1. When redirected to the sign in , there is a form with a HTTP post request to "/users/signin"
2. There is two division created by the two <div>
3. First <label> is for username , there is an input tag that create a params[:username].
The value attribute will display username if there is one already.
CCCC
get "/users/signin" do erb :signin end
Explanaion:
When redirected to the sign in via GET , This will render view/sigin erb page.
post "/users/signin" do if params[:username] == "admin" && params[:password] == "secret" session[:username] = params[:username] session[:message] = "Welcome!" redirect "/" else session[:message] = "Invalid credentials" status 422 erb :signin end end
Explanaion:
When directed to the "/users/signin" page via a HTTP post request, then check if params[:username]
is equal to "admin" and params[:secret] is equal to "secret". If yes, then set username variable to
session[:username]. Also create a key/value hash with the value "Welcome!".
If the username is not "admin" or password is not "secret", or both, then create a key/value has with
the value "Invalid credentials". Assigned a status of 422
Status code 422 :
1. understands the content type of the request entity
2. syntax of the request entity is correct
3. was unable to process the contained instructions
Then render the erb page "signin" again, because it is invalid credential
post "/users/signout" do session.delete(:username) session[:message] = "You have been signed out." redirect "/" end
Explanaion:
In the case of HTTP POST to "users/signout" , then you delete the :username session. Append the
message "You have been signed out."
Redirect to "/".
Comments
Post a Comment