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.
_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)
_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 ...
I understand that the value you specify to send as strong parameters when you send the data as parameters. (Is it correct ...?)
Recommended Posts