Create multiple records at once ruby on rails -


this seemed straightforward, proving not be.

i have 2 models: authors , books. authors has_many books, books belong_to authors. i'd able add many books in single form different authors , information each.

class bookscontroller < applicationcontroller  def new  @books = []   5.times     @books << book.new   end end 

i'm not sure if that's correct way form controller. view follows:

<%= form_tag(books_path(@books)) %>  <% @books.each |book| %>   <%= text_field_tag(:title) %>   <%= text_field_tag(:author) %>   <%= text_field_tag(:pages) %>  <% end %> <%= submit_tag("submit") %>  

that's far i've gotten, can't @books end in params, haven't gotten trying create method yet. i'm getting :title, :author , :pages of last record, , aren't in book param.

form_tag doesn't know model object. won't wrap params under appropriate key. should instead use form_for save trouble of constructing params.

in order create associated objects can use accepts_nested_attributes_for.

in order create multiple objects need send params controller in array. i'd suggest save single record only. code cleaner , can take advantage of rails form helpers such form_for , fields_for instead of constructing html code hand.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -