Ruby on Rails

Rails Forms

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

20 câu hỏi phỏng vấn·
Mid-Level
1

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

Câu trả lời

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?

Câu trả lời

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?

Câu trả lời

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 câu hỏi phỏng vấn

Nắm vững Ruby on Rails cho lần phỏng vấn tiếp theo

Truy cập tất cả câu hỏi, flashcards, bài kiểm tra kỹ thuật, bài tập code review và mô phỏng phỏng vấn.

Bắt đầu miễn phí