[Rails] form_with

Introduction

I will refer to the following article. https://pikawaka.com/rails/form_with


form_with is a ** helper method ** for sending information via rails. By using form_with, you can easily create the HTML required for the input form.

What is a helper method? In Rails, it is a general term for methods used mainly for making HTML tags appear in views and processing text.

The advantage of helper methods is ** Simplified description such as path specification and Ruby embedding Can solve security problems ** Etc.


Writing and tools

** Basic writing style **

How to write form_with when not saving to database


<%= form_with url: "path" do |form| %>
  #Form contents
<% end %>

How to write form_with when saving to database

<%= form_with model:Model class instance do|form| %>
  #Form contents
<% end %>

For example, the following code will display this form in your browser.

ruby:new.html.erb


<%= form_with(model: @user, url: entrys_path, local: true) do |form| %>
  <h1>Enter your user name</h1>
  <div class="field">
    <%= form.label :name, "Name (full-width)" %>
    <%= form.text_field :name %>
  </div>
<% end %>

Image from Gyazo

By the way, with the function of form_with, If the instance of the model class passed as an argument is an instance with contents, the process is assigned to the update action.

On the contrary, if the instance of the model class passed as an argument is an instance with no contents, the process will be distributed to the create action.

Since the process is automatically distributed, the method is not specified.


** How to write an advanced form ** When adding options <%= form_with model:Model class instance,option, do |form| %>

Form contents form.html tag name: column name

Options and methods

Option name Description
url option Specify the path of the request to send the form information.
method option Specify the HTTP method of the request to send the form information. The initial value of the option is: post, so it can be omitted when specifying the post method.
local option Specifies whether to disable remote transmission. If set to true, it will be invalid.

html tag method Usage
form.text_field One-line text posting form
form.text_area Multi-line text post form
form.number_field Generate a numeric input box
form.search_field One-line search form
form.email_field Generate email address input box
form.check_box Generate checkbox without database information
form.collection_check_boxes Generate checkboxes based on database information
form.select Create a choice
form.collection_select Generate choices based on database information
form.file_field Generate file selection box
form.datetime_field Generate date and time input field
form.date_select Generate date selection box
form.hidden_field Hidden form
form.submit Generate submit button

Recommended Posts

[Rails] form_with
Delve into Rails form_with
[Rails] Completely understand form_with
[Rails g.error]
Rails basics
Rails Review 1
Rails API
Rails migration
rails tutorial
About Rails 6
Rails foundation
Rails memorandum
rails tutorial
rails tutorial
rails tutorial
Rails form_with communicates Ajax by default
[Rails] About helper method form_with [Basic]
[Introduction to Rails] form_with (local: true)
rails tutorial
Rails Tips
rails tutorial
form_with scope
[Rails] ActiveRecord
Rails Review 2
[Form with two arguments] rails, form_for / form_with arguments
[Rails] About local: true described in form_with
Rails CSV basics
Rails6 jQuery introduced
About Rails routing
Rails Routing Basics
Add binding.pry (rails)
[Rails] When I use form_with, the screen freezes! ??
Rails access restrictions
Rails and FormData
rails tutorial Chapter 6
Rails tutorial test
Rails render redirect_to
[Rails] Favorite feature
Rails Bootstrap introduced
[Rails] Many-to-many creation
[Rails] Naming convention
Rails Credentials Note
Rails Logger Basics
[Rails] Introducing jquery
[Rails 6] Ranking function
Rails Flash Message
Tailwind on Rails
rails tutorial Chapter 1
Rails delegate method
[Rails 6] cocoon_ introduction
[Rails] Introduce Carrierwave
Rails tutorial memorandum 1
[rails] Set validation
Rails learning day 3
Rails tutorial memorandum 2
Rails learning day 4
Implement Rails pagination
[Rails] Category function
rails tutorial Chapter 5
Group_by in Rails
Rails follow function