Ruby on Rails

110 flashcards by MentisLibrary

Rails as a working developer meets it: the routes file, controllers and strong parameters, Active Record associations and queries, migrations, ERB views and Hotwire, validations, sessions and authentication, Active Job and Minitest.

What is Ruby on Rails?
A full-stack MVC web framework written in Ruby, built around convention over configuration: follow the naming rules and most wiring is inferred rather than declared.
What lives in app/, config/ and db/?
app/ holds models, controllers, views, jobs, mailers and helpers; config/ holds routes, environments and initializers; db/ holds migrations, schema.rb and seeds.
Why run bin/rails rather than rails?
bin/rails is the project's binstub: it loads Bundler and the exact gem versions in Gemfile.lock before running the command.
What does convention over configuration mean in practice?
A Post model reads the posts table, PostsController renders views from app/views/posts/, and the show action renders show.html.erb with no wiring.
What are the three default Rails environments?
development, test and production, each with its own database entry and a settings file under config/environments/.
What is an initializer?
A Ruby file in config/initializers/ run once during boot, after the framework and gems are loaded, used to configure libraries and set constants.
How does Rails store secrets?
In an encrypted file, config/credentials.yml.enc, decrypted with config/master.key and edited through a command that opens your editor.
What do Gemfile and Gemfile.lock do?
Gemfile declares dependencies and version constraints; Gemfile.lock records the exact resolved versions so every machine installs the same set.
How does Rails handle JavaScript and CSS by default?
Rails 7 ships importmap-rails, which maps bare module names to files served directly, with no bundling step; bundler-based setups are available as alternatives.
What does bin/rails console --sandbox do?
Opens the interactive console inside a database transaction that is rolled back when you exit.
Where are routes defined?
In config/routes.rb, mapping an HTTP verb and path to a controller#action string.
What does resources :posts generate?
The seven RESTful routes: index, new, create, show, edit, update and destroy, mapped to the matching PostsController actions.
What are route helpers?
Methods generated from route names that build URLs, so paths are never written as strings in views and controllers.
How does a route pass data to an action?
Dynamic segments written with a leading colon are captured into params under that name.
How do you nest routes?
Put one resources call inside another's block so the child's URL includes the parent's id.
member vs collection routes
A member route acts on one record and includes its id; a collection route acts on the whole set and does not.
How do you set the home page?
With root, which maps GET / to a controller action and defines a root_path helper.
What does namespace do in routes?
Groups routes under a URL prefix and a matching controller module, so namespace :admin looks for Admin::PostsController.
How do you list every route?
bin/rails routes prints the name, verb, URI pattern and controller action for each route, and -g filters the list.
How do you constrain a route?
With constraints, using a regular expression on a segment or a matcher object on the request.
What does a controller action do?
Handles one request: reads params, talks to models, sets instance variables, and either renders a view or redirects.
What is in params?
One object merging the route segments, the query string and the request body, presented as ActionController::Parameters.
What are strong parameters?
A required filter on request data: you name the expected top-level key and the attributes allowed through, and everything else is dropped.
render vs redirect_to
render produces a response from a template in the current request; redirect_to sends a 302 telling the browser to make a new request.
What is a before_action?
A filter that runs before the named actions and can halt the request by rendering or redirecting.
How does flash work?
It stores a short message in the session for exactly the next request, so a redirect can carry a notice to the page that follows.
How does one action serve both HTML and JSON?
With respond_to, giving a block per format; Rails picks the branch from the request's Accept header or the path extension.
How are sessions stored?
By default in a cookie that is signed and encrypted with the application's secret, so the client can read nothing and cannot tamper with it.
How does Rails protect against CSRF?
Every non-GET request from a browser must carry an authenticity token; form_with and button_to include it automatically.
How do you set the response status?
Pass status: to render, or use head when there is no body.