Background
Inside of my application, a series is composed of many books. A series' Show page allows a user to see all the books in a series and to add a new book to the series using a form.
Every book listed on the Show page has a link to an Edit page for that book. The edit page contains the same form used to initially add a book. When editing a book, the form should auto-fill with the books existing information.
Question
How do I configure my form_with
tag so that it can both create a new book and edit an existing book (auto-filling the edit form)? I have tried the following configurations, but they either break the Edit page or break the Show page:
<%= form_with(model: [ @series, @series.books.build ], local: true) do |form| %>
- Breaks book Edit page
- Error: No error, but form doesn't auto-fill data
<%= form_with(model: @book, url: series_book_path, local: true) do |form| %>
- Breaks series Show page
- Error:
No route matches {:action=>"show", :controller=>"books", :id=>"6"}, missing required keys: [series_id]
<%= form_with(model: [:series, @book], local: true) do |form| %>
- Breaks series Show page
- Error:
Undefined method 'model_name' for nil:NilClass
<%= form_with(model: [@series, @series.books.find(@book.id)], local: true) do |form| %>
- Breaks series Show page
- Error:
undefined method 'id' for nil:NilClass
<%= form_with(model: @book, url: [@series, @book], local: true) do |form| %>
- Breaks when submitting new book on series Show page
- Error:
No route matches [POST] "/series/6"
Resources I have consulted:
- https://api.rubyonrails.org/v5.2.2/classes/ActionView/Helpers/FormHelper.html#method-i-form_with
- https://guides.rubyonrails.org/form_helpers.html
- https://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default
- https://github.com/rails/rails/blob/master/actionview/lib/action_view/helpers/form_helper.rb
Existing code
Stripped-down sections of relevant code are below, as well as links to where they exist in my current GitHub repository.
resources :series do
resources :books
end
class Book < ApplicationRecord
belongs_to :series
end
class Series < ApplicationRecord
has_many :books, dependent: :destroy
end
create_table "books", force: :cascade do |t|
t.integer "series_number"
t.integer "year_published"
t.integer "series_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["series_id"], name: "index_books_on_series_id"
end
create_table "series", force: :cascade do |t|
t.string "title"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
app/views/series/show.html.erb
<%= render @series.books %>
<%= render 'books/form' %>
app/views/books/_book.html.erb
<%= link_to 'Edit', edit_series_book_path(book.series, book) %>
<%= render 'form' %>
app/views/books/_form.html.erb
<%= form_with(model: @book, url: [@series, @book], local: true) do |form| %>
<%= form.label :series_number %>
<%= form.number_field :series_number %>
<%= form.label :year_published %>
<%= form.number_field :year_published %>
<% end %>
app/controllers/books_controller.rb
class BooksController < ApplicationController
def index
@books = Book.all
end
def show
@book = Book.find(params[:id])
end
def new
@book = Book.new
end
def edit
@series = Series.find(params[:series_id])
@book = @series.books.find(params[:id])
end
def create
@series = Series.find(params[:series_id])
@book = @series.books.create(book_params)
redirect_to series_path(@series)
end
def destroy
@series = Series.find(params[:series_id])
@book = @series.books.find(params[:id])
@book.destroy
redirect_to series_path(@series)
end
private
def book_params
params.require(:book).permit(:year_published, :series_number)
end
end
Routes
Prefix Verb URI Pattern Controller#Action
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
series_books GET /series/:series_id/books(.:format) books#index
POST /series/:series_id/books(.:format) books#create
new_series_book GET /series/:series_id/books/new(.:format) books#new
edit_series_book GET /series/:series_id/books/:id/edit(.:format) books#edit
series_book GET /series/:series_id/books/:id(.:format) books#show
PATCH /series/:series_id/books/:id(.:format) books#update
PUT /series/:series_id/books/:id(.:format) books#update
DELETE /series/:series_id/books/:id(.:format) books#destroy
series_index GET /series(.:format) series#index
POST /series(.:format) series#create
new_series GET /series/new(.:format) series#new
edit_series GET /series/:id/edit(.:format) series#edit
series GET /series/:id(.:format) series#show
PATCH /series/:id(.:format) series#update
PUT /series/:id(.:format) series#update
DELETE /series/:id(.:format) series#destroy
local: true
option forform_with
has nothing to do with local variables. It just sets thedata-remote
attribute on the form which determines if the form is sent normally or with AJAX. – Aristocrat