[RUBY] [Rails] When I use form_with, the screen freezes! ??

Background

I am a beginner with 3 months of programming experience. The other day, I personally created an application ... It was when I was implementing a message posting form using ** "form_with" **. ..

View


= form_with model: @post, url: posts_path, method: :post, html:{ class: "post" } do |f|
  = f.text_area :post
  = f.submit "Post"

Oh, the view (screen) doesn't change even if you press the "Post" button, right? ??

Checking the model and path shouldn't be wrong ... However, it is not processed by the controller. ..

Yes, I was ** pretending to understand form_with and licking it completely **. (Of course I didn't feel like that) Because I've been studying form_for and form_tag so far, I thought that I could do the same thing just by changing the writing style.

Cause

If you give the answer immediately, form_with will ** automatically perform ajax processing (asynchronous communication) **! !! Of course, form_for and form_tag did not have such specifications.

solution

View


//form_local at the end of with:I will describe true.
= form_with model: @post, url: posts_path, method: :post, html:{ class: "post" }, local: true do |f|
  = f.text_area :post
  = f.submit "Post"

After writing as above, you can post normally.

If you want to do ajax processing ** If ajax processing is not ** not ** ... local: true

Summary

That's it.

At a certain school or ○○ gate, I often study with form_for and form_tag, so Isn't there anyone other than me who falls into this situation? I thought, I wrote this time.

form_for and form_tag are very educational, so try studying once before getting used to form_with. I recommend it, but it's still a different thing, so it was around this time that I felt that form_with also needed to study hard.

Recommended Posts

[Rails] When I use form_with, the screen freezes! ??
[Rails] I don't know how to use the model ...
What should I use for the testing framework [Rails]
I can't get out of the Rails dbconsole screen
[Rails] I could use .sample when I wanted one record at random from the table.
Why can I use the rails command installed with gem? ??
I want to use screen sharing on the login screen on Ubuntu 18
How to use the form_with method
[Rails] I tried deleting the application
[Ruby] This is the solution. When should I use instance variables?
[Rails 5] How to display the password change screen when using devise
[Rails] I was addicted to the nginx settings when using Action Cable.
[Rails] About the error when displaying the screen due to the autofocus of the form
[Rails] How to use the map method
[Rails] I will explain the implementation procedure of the follow function using form_with.
[Rails] form_with
I rewrote the Rails tutorial test with RSpec
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to organize the session in Rails
When the Ruby on Rails terminal rolls back
When you want to use the method outside
The code I used to connect Rails 3 to PostgreSQL 10
After all, how should I use Rails callback?
I summarized the naming conventions for each Rails
When importing CSV with Rails, it was really easy to use the nkf command
[Rails] Use jQuery
I changed the way Rails tutorials run: Rails Tutorial Notes-Chapter 9
[Rails] I learned about the difference between resources and resources
Import the instance and use it on another screen
[Rails] I tried playing with the comment send button
I want to use a little icon in Rails
Implement user follow function in Rails (I use Ajax) ②
I summarized the display format of the JSON response of Rails
Implement user follow function in Rails (I use Ajax) ①
Note that I stumbled upon building the Rails environment
Switch the display screen when hovering the tab with jQuery
[Rails] I investigated the difference between redirect_to and render.
When the server does not start with rails s