Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

Ruby on Rails Module 4

Views, forms and assets interview questions

Interview questions on views, forms and assets: short warm-ups, then what each harder question tests, the first step of a good answer and its follow-ups.

22 questions: 10 warm-ups and 12 with model answers · Levels: Fresher to staff

Warm-up questions

Short questions with short answers, each answered in full.

  1. Fresher

    Which Action View output tag escapes what it prints, and which three ways of printing a value skip that escaping?

    The plain output tag escapes, unless the value it prints is already marked as HTML-safe, so an ampersand, a quote and the angle brackets reach the page as character references and a typed script element is shown as text. Three ways skip it. The raw helper prints the value unchanged, and is only a call to html_safe written differently. The double-equals tag is the same thing in tag form, which makes it easy to misread as the ordinary one. And calling html_safe marks the string itself, so every later output tag prints it as it is, however far from the template that marking happened. For a field that is meant to carry markup, sanitize checks it against an allowlist of tags and attributes and marks only the result.

    From the lesson ERB views, layouts, partials and helpers

  2. Fresher

    On a failed save, why re-render the form with status 422 rather than 200 when Turbo is driving the page?

    Turbo picks its behaviour from the response status. A 2xx is a successful navigation, so Turbo follows it and the re-rendered form with its errors is not swapped into the page. A 4xx or 5xx tells Turbo the submission failed, so it replaces the form in place, keeping the errors and what the user typed. Status 422 Unprocessable Content means the request was understood but its values could not be processed, which is exactly a validation failure. A successful change uses redirect with 303 for the same reason, so Turbo follows it as a navigation. In Rails 8.1 the symbol is :unprocessable_content.

    From the lesson Forms with form_with

  3. Fresher

    Why does f.check_box render a hidden input with value 0 immediately before the checkbox?

    A browser sends a checkbox's value only when it is ticked and nothing when it is not, so without help the controller could not tell an unticked box from a field that was never submitted. f.check_box writes a hidden input valued 0 just before the box, which is valued 1. When the box is unticked only the hidden 0 is sent; when it is ticked both are sent and the later checkbox value 1 wins. So the parameter is always present, 0 or 1, and the model can store a real boolean rather than guess from a missing key.

    From the lesson Forms with form_with

  4. Fresher

    A form object's save returns false, yet the database holds one of the three rows it was supposed to write. Why is that a bug, and how does ActiveRecord::Base.transaction remove it?

    Answered in the lesson Active Model and form objects

  5. Fresher to mid

    What does Propshaft do to an asset's file name, and why does that make long caching safe?

    Answered in the lesson Assets: Propshaft, import maps and CSS

  6. Fresher to mid

    On a new app, bin/importmap audit says no vulnerable packages were found. What did it check?

    Answered in the lesson Assets: Propshaft, import maps and CSS

  7. Fresher to mid

    A nested form adds two guardians and the second has an invalid phone. How many guardians are saved?

    Answered in the lesson Nested forms and custom form builders

  8. Fresher

    Why does collection_check_boxes write a hidden field with an empty value?

    Answered in the lesson Nested forms and custom form builders

  9. Fresher to mid

    When would you use a presenter instead of a helper?

    Answered in the lesson View components, presenters and Phlex

  10. Fresher to mid

    What does ViewComponent's render_inline return, and what can a test do with it?

    Answered in the lesson View components, presenters and Phlex

Questions with model answers

What each question tests, the first step of a good answer and the follow-ups to expect.

  1. Fresher to mid

    A page shows teachers' announcements, which may carry bold text and links, so the template prints the field with the raw helper. A guardian reports a pop-up on that page. How do you make the page safe without losing the formatting, and how do you show that no other page has the same hole?

    What it tests
    Encoding where a value is printed: choosing sanitize over raw, auditing everything that marks data as safe, and proving the fix with a check instead of by reading the markup.
    First step
    List every place that skips escaping: search the views and helpers for raw, for html_safe and for the double-equals tag, and mark which of them print text a person typed. Then decide which tags the announcement really needs; for formatted text that is usually paragraphs, bold, italics and links.
    Follow-ups

    From the lesson ERB views, layouts, partials and helpers

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.

    Opening the model answers with your pass…

    Model answers open with any MySmartCoPilot pass.

    • Pro pass · 1 week₹79$2.99
    • 30 days₹179$5.99
    • 1 year₹1,699$45

    One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.

    It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 5 minutes
  2. Mid to senior

    Three teams keep adding to one layout: a notice bar, a term switcher and a print footer, each wanted on some pages only. Decide where each piece belongs among a controller's own layout, a named block the template fills, and a shared partial, and defend the split.

    What it tests
    Choosing between layout inheritance, named blocks and shared partials from how often a piece varies, who knows whether it is wanted, and what a page that forgets it should show.
    First step
    Ask two questions about each piece. Does every page of one controller want it, or only some pages? And who knows the answer, the controller or the template? Varying by controller points at a layout file, varying by page at a named block with a default, and markup repeated by several controllers at a partial in the application view folder.
    Follow-ups

    From the lesson ERB views, layouts, partials and helpers

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 5 minutes
  3. Mid to senior

    A Rails form shows validation errors in red, but a screen-reader user never learns which field is wrong or why. Describe the markup that fixes it for both a per-field message and a summary, and how you would prove it in a test without a browser.

    What it tests
    Accessible error reporting: per-field aria-invalid and aria-describedby, an error summary, focus, and a request-level test that reads the HTML rather than a browser test.
    First step
    Decide what a screen reader must receive: each broken field marked aria-invalid with aria-describedby pointing at its message, and, for a long form, one summary at the top it can jump to. Then plan a request test that submits an invalid form and parses the response HTML.
    Follow-ups

    From the lesson Forms with form_with

    Model answer, Pro.

    In the answer:

    • Model answer
    • Common mistakes
    • Rubric
    • about 3 minutes
  4. Mid to senior

    A fee form takes an amount in rupees, but the app stores and compares money as whole paise. How do you move between the two in a form safely, and what breaks if you store rupees as a floating-point number?

    What it tests
    Modelling money: a virtual rupees attribute over an integer paise column, exact conversion and rounding, where it runs, and the bugs floats cause in sums and comparisons.
    First step
    Keep one source of truth: an integer paise column. Add a virtual rupees attribute the form reads and writes, and convert in the model before validation, so both the form and any API agree. Then decide how to round and what to reject.
    Follow-ups

    From the lesson Forms with form_with

    Model answer, Pro.

    In the answer:

    • Model answer
    • Common mistakes
    • Rubric
    • about 3 minutes
  5. Fresher to senior

    A Kaksha enrolment screen posts student, guardian and fee-plan fields plus a consent checkbox the student table does not hold. The team wants to know whether the right home for the rules is a form object, extra columns on Student, a service object, or the controller. Pick one and defend it against the other three.

    What it tests
    When a plain Active Model form object is the right home for a screen's rules, and why the three alternatives (fat model, service object, fat controller) do worse on testability, cohesion and reuse.
    First step
    List what the screen owns that no single table does: fields across models, cross-record rules, a one-screen consent, an atomic save. Then match each alternative to those four needs and name the one it fails on.
    Follow-ups

    From the lesson Active Model and form objects

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 5 minutes
  6. Fresher to staff

    The lesson's first program builds Guardian on ActiveModel::API and ActiveModel::Attributes, while the Guardian behind its saving form is an Active Record model. A teammate asks which is right. Explain the trade: what each stack supplies, when the plain Active Model class is right, and when it is the wrong choice.

    What it tests
    What ActiveModel::API, ActiveModel::Attributes and ActiveModel::Attributes::Normalization supply, where the line between Active Model and Active Record sits, and how to pick for a given screen or responsibility.
    First step
    Separate what Active Record supplies (connection, schema, SQL, callbacks, associations) from what Active Model supplies (naming, validations, type casting, errors, normalizes), then say which the screen actually needs.
    Follow-ups

    From the lesson Active Model and form objects

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 5 minutes
  7. Mid to senior

    A Rails 8.1 app runs on import maps and Propshaft, with a dozen Stimulus controllers. A new fee dashboard is to be written as React components in JSX. What do you change, what happens to the existing controllers, and how does the deploy change?

    What it tests
    Knowing that import maps compile nothing, which gem adds a bundler, where its output goes, and what moving Stimulus controllers into a bundle involves.
    First step
    Name what JSX needs that the default stack lacks, a compile step, then pick the gem that adds one and say where its output lands and who serves it from there.
    Follow-ups

    From the lesson Assets: Propshaft, import maps and CSS

    Model answer, Pro.

    In the answer:

    • Model answer
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  8. Mid to senior

    You turn on a nonce-based Content Security Policy in a Rails 8.1 app. A security auditor asks what it protects against, what it does not, and how you would show that it works before the release. Answer them.

    What it tests
    How a nonce is made and checked, which tags Rails gives it, why an injected script fails, the limits of the policy, and a check that proves it.
    First step
    Say where the nonce appears (the header and the allowed tags), who makes it and how often, and what the browser does with a script tag whose nonce is missing or wrong.
    Follow-ups

    From the lesson Assets: Propshaft, import maps and CSS

    Model answer, Pro.

    In the answer:

    • Model answer
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  9. Mid to senior

    Every Kaksha student must keep at least one guardian, but the admission form may remove a guardian and add another in the same request. Where does the rule go, how do you write it, and how do you show it handles both cases?

    What it tests
    Putting a rule that spans children on the parent model, counting children as they will be after the save, and proving it with runs of the edge cases.
    First step
    Decide what "at least one" means for a nested save: the children that will exist after it, which are the loaded and new ones not marked for destruction. Then list the requests to try: remove the only one, swap one for another, create a student with none.
    Follow-ups

    From the lesson Nested forms and custom form builders

    Model answer, Pro.

    In the answer:

    • Model answer
    • Common mistakes
    • Rubric
    • Variants
    • about 2 minutes
  10. Mid to senior

    Kaksha's FormBuilder should pick the input for each attribute itself: a date field for a date, a number field for an integer, a check box for a boolean, a text field otherwise. How would you write that method, and where does it stop being a good idea?

    What it tests
    Reading an attribute's type from inside a builder through the object, covering form objects that are not tables, and knowing when an explicit input type is clearer.
    First step
    Find where the type lives: the classes of Active Record models and of Active Model objects with typed attributes both answer type_for_attribute, so the builder asks object.class. Then map a few types to field helpers and keep a fallback for anything else.
    Follow-ups

    From the lesson Nested forms and custom form builders

    Model answer, Pro.

    In the answer:

    • Model answer
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  11. Mid to senior

    A Rails app has 40 partials; 3 of them are copied across two apps and have many states. The team asks whether to adopt ViewComponent, Phlex, or neither. What do you ask first, what do you recommend, and how do you roll it out?

    What it tests
    Telling formatting pain from structure pain, weighing a gem against what partials and strict locals already give, and planning a gradual adoption.
    First step
    Find where the pain is: formatting that repeats (a presenter), shared markup with many states and no tests (a component), or a team that would rather write views in Ruby (Phlex). Then count which partials actually have it.
    Follow-ups

    From the lesson View components, presenters and Phlex

    Model answer, Pro.

    In the answer:

    • Model answer
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  12. Mid to staff

    A security audit of an app that uses partials, ViewComponent and Phlex asks how output escaping works in each and where it can be bypassed. Explain it, and describe how you would check the code base.

    What it tests
    Knowing that all three escape by default, naming each explicit opt-out, spotting double escaping, and turning the search for opt-outs into a repeatable check.
    First step
    State the default of each tool, then list the one explicit way each prints markup unescaped, then decide which of those places can ever receive text a person typed.
    Follow-ups

    From the lesson View components, presenters and Phlex

    Model answer, Pro.

    In the answer:

    • Model answer
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes

All Ruby on Rails interview questions · The Ruby on Rails track

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.