There are book and author models.
In the books/new view you can enter the name of the book and the name of the author.
How to make it so that when you click on the "add book" button, both the book object and the author object are created?
There are book and author models.
In the books/new view you can enter the name of the book and the name of the author.
How to make it so that when you click on the "add book" button, both the book object and the author object are created?
Suppose this is your model
class Book < ActiveRecord::Base has_one :author accepts_nested_attributes_for :author # ^^^ эта строчка нужна, что бы автор создавался автоматически end this is your view
<%= form_for @book, url: {action: "create"} do |f| %> <%= f.text_field :name %> <%= fields_for @book.author do |ff| %> <%= ff.text_field :name %> <% end %> <% end %> then in the controller
book = Book.create(params[:book]) Source: https://ru.stackoverflow.com/questions/624220/
All Articles