背景
在我的应用程序中,一个系列由许多书籍组成。系列的展示页面允许用户查看该系列中的所有书籍,并使用表单向该系列添加新书。
在展示页面上列出的每本书都有一个链接到该书的编辑页面。编辑页面包含与最初添加书籍时相同的表单。在编辑书籍时,表单应自动填充书籍的现有信息。
问题
如何配置form_with
标签,以便它既可以创建新书籍,又可以编辑现有书籍(自动填充编辑表单)?我尝试了以下配置,但它们要么破坏了编辑页面,要么破坏了展示页面:
<%= form_with(model: [ @series, @series.books.build ], local: true) do |form| %>
- 破坏了书籍编辑页面
- 错误:没有错误,但表单不会自动填充数据
<%= form_with(model: @book, url: series_book_path, local: true) do |form| %>
- 破坏了系列展示页面
- 错误:
No route matches {:action=>"show", :controller=>"books", :id=>"6"}, missing required keys: [series_id]
<%= form_with(model: [:series, @book], local: true) do |form| %>
- 破坏了系列展示页面
- 错误:
Undefined method 'model_name' for nil:NilClass
<%= form_with(model: [@series, @series.books.find(@book.id)], local: true) do |form| %>
- 破坏了系列展示页面
- 错误:
undefined method 'id' for nil:NilClass
<%= form_with(model: @book, url: [@series, @book], local: true) do |form| %>
- 在系列展示页面上提交新书时出错
- 错误:
No route matches [POST] "/series/6"
我参考的资源:
- 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
现有代码
以下是相关代码的简化部分,以及它们在我的GitHub存储库中的链接。
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
路由
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
form_with
中的local: true
选项与本地变量无关。它只是设置表单上的data-remote
属性,该属性决定表单是正常发送还是使用AJAX方式发送。 - maxshow.html.erb
代码为<%= render partial: 'books/form', locals: {model: [@series, @series.books.new]} %>
以使其正常工作。当使用局部变量时,似乎需要明确说明它是一个部分。 - Taylor Liss