Chapter 8 Flashcards
What is a stateless protocol?
A stateless protocol treats each request as an independent transaction that is unable to remember information from any previous requests. This is true of HTTP.
What is a session?
A semi-permanent connection between two computers (such as a client computer running a web browser and a server running Rails).
What is a cookie?
A small piece of text placed on a user’s browser, which allows sessions to be maintained after the browser has been closed (i.e. the application can quickly retrieve the logged in user ID based on the provided cookie)
How can the elements of logging in and out correspond the REST actions?
A login form is handled by the ‘new’ action (GET), actually logging in sends a POST request to the ‘create’ action, and logging out is handled by sending a DELETE request to the ‘destroy’ action (assuming this is a temporary session w/o cookies)
In the following generate command for a Sessions controller, why do we create an action for ‘new’ and not ‘create’ or ‘delete’:
$ rails generate controller Sessions new
B/C only the ‘new’ action requires a view (we’re GETting a form). The other two do not render a view (at best they may flash a message). Since the generator makes views for all actions given as arguments, it is not necessary to generate the other two functions.
The login page creates a session. If 2 methods (new, create) correspond to the /login url, how can we route the two methods to different controller actions?
Use a different HTTP method for each:
get ‘login’ => ‘sessions#new’
post ‘login’ => ‘sessions#create’
delete ‘logout’ => ‘sessions#destroy’
What is the command to view all of the application’s routes?
$bundle exec rake routes
Why does a generated controller not have access to Active Record’s functions?
B/C generated models inherit from Active Record, generated controllers do not.
When creating a login page corresponding to our Sessions controller, why can’t we just pass the form_for function a variable.
B/C there is no corresponding model, so we cannot set an @sessions variable in the erb.
Explain the following action:
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
# Log the user in and redirect to the user’s show page.
else
# Create an error message.
render ‘new’
end
end
Set the user variable to a user corresponding to the values passed to the find_by method (from Active Record). The if statement makes sense b/c ‘user’ will be nil if no matching user is found, and remember that, along with false, nil is the only object in ruby that is equivalent to a boolean false.
What is flash persistence, and what can cause it?
It is when a flash message continues to appear after it should have disappeared. One possible cause is that re-rendering a template with ‘render’ doesn’t count as a request, which is what would cause the flash to vanish.
Explain the following test of a login page:
test "login with invalid information" do get login_path assert_template 'sessions/new' post login_path, session: { email: "", password: "" } assert_template 'sessions/new' assert_not flash.empty? get root_path assert flash.empty? end
Visit the login path.
Verify that the new sessions form renders properly.
Post to the sessions path with an invalid params hash.
Verify that the new sessions form gets re-rendered and that a flash message appears.
Visit another page (such as the Home page).
Verify that the flash message doesn’t appear on the new page.
What is the syntax to run a single test instead of a full battery?
$ bundle exec rake test TEST=test/fulltestpath…/testname.rb
What is the point of ‘flash.now’?
It disappears as soon as any additional request is received, avoiding flash persistence.
T/F: helpers are made available in both their corresponding view and controller.
F; the helper module is included automatically in the view but not in the controller. It can be added, however, with an ‘include’ statement.