[Rails] Two ways to write form_with

This article uses Ruby 2.6.5 installed on macOS Catalina 10.15.6.

What is form_with

--A helper method for sending information. --It will display the HTML required for input. --There are two ways to write form_with. ――The first is __ when the input information is saved in the database __, and the second is __ when the input information is not saved in the database __.

When saving to database

--model: followed by __ a model class instance __. --Create a text box with f.text_field.

<%= form_with model: @book, local: true do |f| %>  
    <%= f.text_field :keyword, placeholder: "keyword", class: "search-input"%>
    <%= f.submit "search", class: "search-btn" %>
<% end %>

When not saving to database

--ʻUrl: `followed by __ information destination path __.

<%= form_with url: search_path, local: true do |f| %>
   <%= f.text_field :keyword, placeholder: "keyword", class: "search-input"%>
   <%= f.submit "search", class: "search-btn" %>
<% end %>

When nested in the routing

--For example, let's say the post controller has nested comment controller routing. ――In that case, the code looks like this. --_ You should describe two instances of the model class in the order of parent class and child class __.

<%= form_with model: [@post, @comment] do |f| %>
  <%= f.text_field :comment%>
  <%= f.submit %>
<% end %>


https://pikawaka.com/rails/form_with https://qiita.com/hmmrjn/items/24f3b8eade206ace17e2

Recommended Posts

[Rails] Two ways to write form_with
How to write Rails
[Rails] Various ways to write in seed files
How to write Rails seed
[Rails] Various ways to delete data
[Rails] How to write exception handling?
[Form with two arguments] rails, form_for / form_with arguments
[Rails] How to write when making a subquery
[Android] Two ways to get a Bluetooth Adapter
[Ruby on Rails] How to write enum in Japanese
Introducing Bootstrap to Rails 5
Introducing Bootstrap to Rails !!
Introduce Vue.js to Rails
How to write a date comparison search in Rails
How to write Mockito
How to write migrationfile
[Rails] Completely understand form_with
How to write a migration from Rails datetime type to date type
[Rails] How to write user_id (foreign key) in strong parameter
How to write good code
[Rails] How to use enum
[Rails] How to use enum
How to write java comments
How to read rails routes
[Refactoring] How to write routing
How to use rails join
[Rails] Add column to devise
How to write Junit 5 organized
How to terminate rails server
[Rails] How to use validation
To write a user-oriented program (1)
[Rails] How to disable turbolinks
Pass parameters to Rails link_to
[Rails] How to use authenticate_user!
[Rails] How to implement scraping
[Rails] How to make seed
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
Introduced gRPC client to rails
[Rails] How to use Scope
[RSpec on Rails] How to write test code for beginners by beginners
[Java] How to write when passing two or more arguments to super
[Rails] What to do if you can't get parameters with form_with