[RUBY] Summary of stumbling blocks related to form_with

Introduction

When I went around the Rails tutorial (version 5.1 compatible) and made a portfolio, I wrote an article as a memorandum because I got stuck with form _with. I would appreciate it if you could comment if there are any mistakes.

I will summarize each error that occurred below.

flash is not displayed

Create a form to post the following articles

_post_form.html.erb


<%= form_with model: @post, url: yield(:form_url), method: yield(:method),
  local: true, multipart: true do |form| %>

・ ・ ・ Various forms

<% end %>

At first, I wrote it without setting local: true, but I was in trouble because I could not display the flash of successful posting. Apparently, form_with communicates using Ajax by default, which makes it impossible to display flash. (I do not understand the detailed principle)

File cannot be sent

This happened when creating a form to submit a pdf file like this:

_post_form.html.erb


<%= form_with model: @post, url: yield(:form_url), method: yield(:method),
  local: true, multipart: true do |form| %>

... other forms ...

<%= form.label :file, "pdf file", class: "control-label" %>
<%= form.file_field :file, class: "form-control-file" %>

<% end %>

We have introduced carrierwave for sending pdf files. The carrierwave settings will be summarized in a separate article. This time, it's a story after that. When I googled because the form could not be sent, I did not specify multipart: true in the argument of form_with. If you specify this, it seems that you can also send the file format. In Rails 4 and later, I wrote that multipart: true is implicitly specified if there is a description of file_field, but for some reason I was not done ...

About scope option

I referred to the following article. https://qiita.com/akilax/items/f36b13f377f7e442bc73

I understand that the value you specify to send as strong parameters when you send the data as parameters. (Is it correct ...?)

At the end

I'm still not familiar with it, so I'll add it as soon as there are new discoveries. form_with It's deep ...

Recommended Posts

Summary of stumbling blocks related to form_with
[Personal memo] Summary of stumbling blocks when deploying Rails apps to AWS
[Java Silver] Summary of points related to lambda expressions
[java] Summary of how to handle char
Summary of how to write annotation arguments
Summary of going to JJUG CCC 2019 Spring
List of recommended articles related to Corda
[Java] [Maven3] Summary of how to use Maven3
Summary of how to select elements in Selenium
A summary of only Rails tutorial setup related
[java] Summary of how to handle character strings
Summary of how to create JSF self-made tags
[Docker] Introduction to docker compose Basic summary of docker-compose.yml
[Java] Summary of how to abbreviate lambda expressions
Java related summary
Summary of moss when updating from JMockit 1.4 to 1.30
Summary of knowledge required to pass Java SE8 Silver
Summary of Java communication API (1) How to use Socket
[Eclipse] Summary of environment settings * Updated from time to time
Summary of Java communication API (3) How to use SocketChannel
Summary of Java communication API (2) How to use HttpUrlConnection
Summary of how to implement default arguments in Java
[Meaning of model: of form_with]
Summary of OpenJDK sources
Summary of strong parameters
Summary of jar files
Summary of information security
Summary of using FragmentArgs
About form. ○○ of form_with
Summary of using DBFlow
Summary of Java support 2018