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.
ruby 2.5.7
Rails 5.2.4.3
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.
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