[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 %>

reference

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 validation
How to write Rails seed
How to write Rails routing
[Rails] How to write in Japanese
[Rails] Various ways to delete data
Rails on Tiles (how to write)
[Rails] How to write exception handling?
[Introduction to Rails] form_with (local: true)
[Rails] form_with
[Form with two arguments] rails, form_for / form_with arguments
Rails: How to write a rake task nicely
Two ways to start a thread in Java + @
[Rails] How to write when making a subquery
[Android] Two ways to get a Bluetooth Adapter
[Rails / JavaScript / Ajax] I tried to create a like function in two ways.
[Ruby on Rails] How to write enum in Japanese
Introducing CircleCI to Rails
Introducing Bootstrap to Rails 5
Introducing Bootstrap to Rails !!
Introduce Vue.js to Rails
How to write dockerfile
How to uninstall Rails
How to write docker-compose
How to write a date comparison search in Rails
How to write Mockito
How to write migrationfile
Delve into Rails form_with
[Rails] Completely understand form_with
How to write a migration from Rails datetime type to date type
[Rails5] japanMap link How to write parameters in js.erb file
[Rails] How to write user_id (foreign key) in strong parameter
[rails] How to post images
How to write good code
[Rails] How to use enum
Bit Tetris (how to write)
[Rails] How to install devise
[Rails] How to use enum
How to write java comments
How to read rails routes
[Refactoring] How to write routing
Great poor (how to write)
[Note] How to write Dockerfile/docker-compose.yml
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)
[Ruby] How to write blocks
[Rails] How to disable turbolinks
Pass parameters to Rails link_to
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[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