[Form_with] Let's unify form with form_with.

【goal】

Understand form_with

【merit】

■ Rails recommendation is "form_with" ■ It's not that difficult, so you should remember

【Development environment】

■ Mac OS catalina ■ Ruby on Rails (5.2.4.2) ■ Virtual Box:6.1 ■ Vagrant: 2.2.7

【the difference】

■form_for There is a model. Used for data you want to save in DB

■form_tag No model Used when performing POST processing

■form_with Both swords A convenient method that combines the above two functions

[I actually used it]

When there is a model

Model name: Book Column name ①: name Column name ②: title

books/index.html.erb



<% form_with model:Book do |f| %> #model postscript
  <%= f.text_field :name %>
  <%= f.text_area :text %>
  <%= f.submit %>
<% end %>

When there is no model

hoge.html.erb


<%= form_with url:hoge_path do |f| %> #Add path name
  <%= f.text_area :name %>
  <%= f.text_area :question %>
  <%= f.submit %>
<% end %>

that's all.

** If there is a model, specify the model name If there is no model, specify by path name **

I also escaped from using it It was really easy, so please use it if you like.

Recommended Posts

[Form_with] Let's unify form with form_with.
[Form with two arguments] rails, form_for / form_with arguments
Let's scrape with Java! !!
About form. ○○ of form_with
Let's experiment with Java inlining
Let's operate Excel with Java! !!
Form part attributes of form_with
Implement search function with form_with
Let's use Amazon Textract with Ruby
Image processing: Let's play with the image
Let's unit test with [rails] Rspec!