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.
Warm-up questions
Short questions with short answers, each answered in full.
-
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
-
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
-
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?
-
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
-
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
-
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
-
Fresher to mid
When would you use a presenter instead of a helper?
Answered in the lesson View components, presenters and Phlex
-
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.
-
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
-
- The stored announcements already hold script elements. What do you do about those rows?
- A designer asks for a style attribute on the allowlist. How do you answer?
- What check would catch the next raw that someone adds to a template?
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.
Passes can’t be bought right now, so the model answers can’t be opened at the moment. If you have a pass, enter your licence key.
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
-
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
-
- Where should focus go after a failed submit, and why not onto the first field?
- How do you report an error on a group of radio buttons rather than one input?
- The messages must read in Hindi as well. Where do the strings live?
- Why is a request test enough here, and when would you still want a browser test?
From the lesson Forms with form_with
Model answer, Pro.In the answer:
- Model answer
- Common mistakes
- Rubric
- about 3 minutes
-
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
-
- Where exactly should the conversion run so the form, validations and errors all agree?
- A parent types 1500.5 rupees. What should happen, and where?
- How do you show the stored paise back to the user as rupees?
- Why not a decimal column of rupees instead of integer paise?
From the lesson Forms with form_with
Model answer, Pro.In the answer:
- Model answer
- Common mistakes
- Rubric
- about 3 minutes
-
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
-
- A teammate wants to drop the form object and put the consent on the students table. What do you say about the first time a screen asks for consent on a different model?
- How does the form object stay thin as the screen grows a seventh field and a fourth validation that spans records?
- A teammate asks why not a service object such as EnrolStudent.call. What does the form object give that the service does not?
- The screen also sends an SMS on success. Does the SMS belong in the form object's save?
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
-
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
-
- A later story needs guardians to be found by phone across requests. Does that change which stack Guardian should use, and what moves with it?
- A teammate adds validates :phone, uniqueness: true to the plain Active Model Guardian. What happens, and where does that rule belong?
- How does ActiveModel::Attributes::Normalization in 8.1 change where you can put a normalizer that used to live on an Active Record model?
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
-
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
-
- If you add jsbundling-rails, what happens to the Stimulus controllers that import maps loaded until now?
- TypeScript also needs compiling. Does the same answer apply?
- How do the build and the deploy run, in development and in production?
From the lesson Assets: Propshaft, import maps and CSS
Model answer, Pro.In the answer:
- Model answer
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
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
-
- If an attacker can read one page's HTML, can they reuse its nonce?
- What happens to an inline script typed by hand in a template?
- How do you check the policy in a test before you deploy?
From the lesson Assets: Propshaft, import maps and CSS
Model answer, Pro.In the answer:
- Model answer
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
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
-
- Why does a check on guardians.count let the last guardian be removed?
- Should the controller refuse a body with no guardian rows?
- The team also wants at most three guardians. Does the same validation cover it?
From the lesson Nested forms and custom form builders
Model answer, Pro.In the answer:
- Model answer
- Common mistakes
- Rubric
- Variants
- about 2 minutes
-
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
-
- What does the method do for an attribute that has no column or declared type?
- A phone is stored as a string. How does the caller get a telephone field?
- Would you put this method in the default builder of every form?
From the lesson Nested forms and custom form builders
Model answer, Pro.In the answer:
- Model answer
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
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
-
- What would you do with the other 37 partials?
- Does Turbo change the decision?
- What does each choice cost the team to keep up to date?
From the lesson View components, presenters and Phlex
Model answer, Pro.In the answer:
- Model answer
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
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
-
- A presenter returns text that a helper has already escaped, and the page shows <. What happened?
- How does Phlex let markup through, and what happens if you try with a plain string?
- How do you keep a new opt-out from slipping in later?
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