Ruby on Rails

Rails Forms

form_with, model forms, form_for legacy, strong parameters, nested attributes, file uploads

20 Interview-Fragen·
Mid-Level
1

What is the recommended form helper in Rails 5.1+ for creating forms?

Antwort

form_with is the unified helper introduced in Rails 5.1 that replaces form_for and form_tag. It generates forms with remote: true by default (AJAX requests) and simplifies syntax by automatically detecting if a model object is passed. This unification reduces confusion between the two older helpers and provides a more consistent API.

2

What is the difference between form_with model: @user and form_with url: users_path?

Antwort

With model: @user, Rails automatically determines the URL and HTTP method based on the object's state (new_record? or persisted?). For a new record, it uses POST to users_path, for an existing one, PATCH to user_path(@user). With url: users_path, the URL and appropriate HTTP method must be specified manually.

3

How to disable the default AJAX submissions of form_with to get a classic HTML submission?

Antwort

By default, form_with generates forms with data-remote='true' that send AJAX requests. To get a traditional HTML submission with page reload, local: true must be added. Starting from Rails 6.1, the default behavior can be configured globally via config.action_view.form_with_generates_remote_forms.

4

What is the role of strong parameters in the context of Rails forms?

5

How to correctly define a strong parameters method for an Article model with title and content?

+17 Interview-Fragen

Meistere Ruby on Rails für dein nächstes Interview

Zugang zu allen Fragen, Flashcards, technischen Tests, Code-Review-Übungen und Interview-Simulatoren.

Kostenlos starten