私はこれに苦労しています。基本的には、2 つのフォームを 1 つのモデルに送信する必要があります。最初のフォームが完了すると、モデルが必要とする残りの情報を含む 2 番目のフォームがレンダリングされます。理想的には、返された 2 つのハッシュを結合して、一度にまとめて保存したいと考えています。これを行うより賢明な方法があれば、私に知らせてください。前もって感謝します。
app/controllers/books_controller.rb
class BooksController < ApplicationController
def new
@book = @library.books.build
@user = current_user
end
def create
@book = @library.books.build(params[:book])
if @book.total_pages.nil?
@book_first_page = @book
render 'new'
else
@book.update_attributes(@book_first_page)
if @book.save
redirect_to root_path
else
render 'new'
end
end
end
end
app/views/books/new.html.erb
<% if @book.title.nil? %>
<%= form_for [:library, @book] do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="form-group input-group-sm">
<%= f.label :title, 'What is the title of the book?' %>
<%= f.text_area :title, class: "form-control" %>
</div>
<%= f.submit "Submit Title", class: "btn btn-large btn-primary" %>
<% end %>
<% else %>
<%= form_for [:library, @book] do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="form-group input-group-sm">
<%= f.label :total_pages, "How many pages?:" %>
<%= f.text_field :total_pages, class: "form-control" %>
</div>
<%= f.submit "Submit Number of Pages", class: "btn btn-large btn-primary" %>
<% end %>