S
S
Sm4llDev2020-08-17 12:17:07
Ruby on Rails
Sm4llDev, 2020-08-17 12:17:07

Syntax and working of form_with in Ruby on Rails 6?

I tried to understand how form_with works, I looked for Ruby on Rails Docs , RusRails , but I didn’t understand what was happening:

<%= form_with scope: :article, url: articles_path, local: true do |form| %>
  <p>
    <%= form.label :title %><br>
    <%= form.text_field :title %>
  </p>

  <p>
    <%= form.label :text %><br>
    <%= form.text_area :text %>
  </p>

  <p>
    <%= form.submit %>
  </p>
<% end %>


If it's not difficult, then you can explain the syntax of the moments, how it works, and what is the "crutch" with the variable |form|?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Mirilaczvili, 2020-08-17
@Sm4llDev

If it's not difficult, then you can explain the syntax of the moments, how it works, and what is the "crutch" with the variable |form|?

If you don't understand how to work with blocks in Ruby, I advise you to read the basics
https://www.rubyguides.com/2016/02/ruby-procs-and-...
form_with(...) do |form|
end

This code generates a form with the fields needed to create and modify an Article. To understand the magic of articles_path, you need to play around with the team rails routesand understand what CRUD, REST are.
You can see what form code is generated by generation in the browser.
Examples can be found here:
https://apidock.com/rails/v6.0.0/ActionView/Helper...
# Adding a scope prefixes the input field names:

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question