我正在努力解决这个问题,基本上我想要两个表单提交给一个模型。当第一个表单完成后,将呈现第二个表单,其中将包含模型所需的剩余信息。理想情况下,我想将两个返回的哈希组合起来,并一次将它们保存在一起。如果有更明智的方法,请告诉我。提前致谢。
应用程序/控制器/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 %>