w4d3 - rails views Flashcards
How do you validate uniqueness of a combination?
validates :attribute, uniqueness: { scope: :attribute2 }
What do views do?
Generate and populate the Response object
What context is a view evaluated in?
the controller
How do you render a view named show.html.erb?
render :show
How do you delete the current line in atom?
CMD + X
What’s the relationship between the new/create actions?
new - render a form
create - receive the form data and act on it
What’s the relationship between the edit/update actions?
edit - renders the form
update - receives form data and acts on it
What params would you send into a url helper?
wildcards
note that if you send in a model, its id will be populated automatically
What are the two ways of setting up a label?
For:
[label for=’id_of_thing’]body[/label]
Subling/wrap:
[label]body[input type=’text’/][/label]
What’s the difference between #update_attributes and #update?
They’re the same (alias for one another)
Where do you generally redirect to?
The model_url helper, where model is the name of the model
How can a form send an array back within the post params?
input name=’model[array_name][ ]’
Use this multiple times within the same form.
What’s the primary function of cookies?
To preserve state between HTTP requests
What are the 2 primary distinctions between hashing functions and cryptographic hashing functions?
- Speed. Cryptographic is slower.
2. Collisions. Cryptographic creates far fewer collisions.
How do you see the class inheritance path for an object?
objectname.class.ancestors
How do you access cookies that are stored only until the next request cycle?
How would you render this in a view?
flash[:some_error] = ‘whatever’
[%= flash[:some_error] %]
What’s the principle behind CSRF protection?
Each form should have a param with a unique code that is associated with the user’s session.
When a form is submitted, if the code does not match up, we assume a CSRF attempt and raise an error.
How does rails implement CSRF protection?
in every one of your forms, you add an input/hidden with
name=”authenticity_token”
and
value =
[%= form_authenticity_token %]
How do you enable automatic CSRF protection per controller? Per site?
- -per controller:
self. per_form_csrf_tokens = true
–per site:
config/application.rb:
Rails.configuration.action_controller.per_form_csrf_tokens = true