[RUBY] [Rails] The cause of not being able to post was in form_with

Introduction

Previously, a student who was creating something like twitter asked me, "I can't post even though I'm pressing a button! I don't get any error text! (Tears)." I had a hard time finding it, so I will post it for people with similar problems.

version

ruby 2.5.7
Rails 5.2.4.3

Cause and solution

books_controller.rb


def create
   @book = Book.new(book_params)
   @book.user_id = current_user.id
   if @book.save
      redirect_to book_path(@book)
   else
      @books = Book.all
      render 'index'
end

... and if you can save to the controller, go to the book_path (@book) page! If you can't, go to the index page! Even though I have specified

ruby:_list.html.erb


<%= form_with model:book, url:root_path do |f| %>
   
<% end %>

... and go to the root_path page when the button is pressed in the view! Another mysterious designation was made.

So

ruby:_list.html.erb


<%= form_with model:book do |f| %>
   
<% end %>

If you delete all the url part of the view like ..., you can post without any problem. Congratulations.

Finally

If the description of the url part of form_with is incorrect, it may cause a routing error, so be careful.

If you want to know more about how to use form_with, we recommend the material here.

Recommended Posts

[Rails] The cause of not being able to post was in form_with
The story when the test folder was not created in Rails
[Note] What to do if bundle install in Chapter 3 of the rails tutorial is not possible
Get the result of POST in Java
The identity of params [: id] in rails
The process of introducing Vuetify to Rails
How to decorate the radio button of rails6 form_with (helper) with CSS
How to change the maximum and maximum number of POST data in Spark
[CircleCI] I was addicted to the automatic test of CircleCI (rails + mysql) [Memo]
Problem of not being able to db: migrate when adding Sorcery submodules
How to constrain the action of the transition destination when not logged in
[Rails] Articles for beginners to organize and understand the flow of form_with
Change the save destination of the image to S3 in the Rails app. Part 2
[Order method] Set the order of data in Rails
How to find the cause of the Ruby error
I tried to organize the session in Rails
What to do when rails db: seed does not reflect in the database
How to use JQuery in js.erb of Rails6
How to make a unique combination of data in the rails intermediate table
[Rails] Added in devise: username not added to database
Is it mainstream not to write the closing tag of <P> tag in Javadoc?
How to check Rails commands in the terminal
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
[Swift] How to simply describe dismiss that was not taught in the introductory book
[rails devise] How to transition users who are not logged in to the login page
For those who want to use MySQL for the database in the environment construction of Rails6 ~.
Install multiple submit buttons in Rails View to get the value of the pressed button
Library not loaded when trying to upgrade the version of ruby and rails s
[Rails] I want to display the link destination of link_to in a separate tab
The part I was addicted to in "Introduction to Ajax in Java Web Applications" of NetBeans
[Rails] How to display the weather forecast of the registered address in Japanese using OpenWeatherMap
I was addicted to the NoSuchMethodError in Cloud Endpoints
How to set the display time to Japan time in Rails
Understand the characteristics of Scala in 5 minutes (Introduction to Scala)
[Rails] How to change the column name of the table
[Rails] How to get the contents of strong parameters
SSL in the local environment of Docker / Rails / puma
[Rails] How to display an image in the view
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
How to solve the problem when the value is not sent when the form is disabled in rails and sent
What to do when Rails on Docker does not reflect controller changes in the browser
[Rails] How to temporarily save the request URL of a user who is not logged in and return to that URL after logging in
A story I was addicted to in Rails validation settings
I was addicted to the setting of laradock + VSCode + xdebug
[Rails] When the layout change of devise is not reflected
[Rails] How to display the list of posts by category
Contributed to Gradle and was named in the release notes
How to derive the last day of the month in Java
[Rails] How to display information stored in the database in view
802.1X authentication to the network of Bonding setting in CentOS7
What to do when "relation" hibernate_sequence "does not exist" in the ID column of PostgreSQL + JPA
[Rough explanation] Causes and remedies for not being able to obtain the name from the ActiveHash model
I tried to make the "Select File" button of the sample application created in the Rails tutorial cool
In order not to confuse the understanding of getters and setters, [Do not use accessors for anything! ]
I want to know the Method of the Controller where the Exception was thrown in the ExceptionHandler of Spring Boot
Method definition location Summary of how to check When defined in the project and Rails / Gem
[rails] How to post images
I tried to implement Ajax processing of like function in Rails
[Rails] How to get the URL of the transition source and redirect
I tried to sort the data in descending order, ascending order / Rails
[Rails] What to do if data is not registered in DB