Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populating associations with empty objects #27

Closed
otagi opened this issue Jul 19, 2016 · 4 comments
Closed

Populating associations with empty objects #27

otagi opened this issue Jul 19, 2016 · 4 comments
Labels

Comments

@otagi
Copy link

otagi commented Jul 19, 2016

I've looked at all the docs I could find, but still cannot figure out how to do the following. Sorry if I'm missing the obvious.

Let's say we have the following code:

class Book < ActiveRecord::Base
  belongs_to :library
end

class Library < ActiveRecord::Base
  has_many :books
  accepts_nested_attributes_for :books
end

class BookForm < Rectify::Form
  attribute :title, String
end

class LibraryForm < Rectify::Form
  attribute :books, Array[BookForm]
end

I want to pre-populate the library form with 5 empty book slots by default, so I can fill 5 books at a time in the view.

Is there a way to add new Book objects to the form, before displaying the view?

@andypike
Copy link
Owner

Hi @otagi,

Thanks for the question. Off the top of my head (without testing it first) I think something like this might work:

MyController < ApplicationController
  def new
    @form = LibraryForm.new(:books => Array.new(5) { BookForm.new })
  end
end

When you instantiate a form object, you can pass in attributes. You just need to set the #books attribute to an Array of 5 elements, each with a different BookForm.

Thinking about it, you might not need different BookForm objects in the array. So maybe try:

:books => Array.new(5, BookForm.new)

I'd be interested in how that goes. If you have problems, please let me know and I'll write a test case for it 😄

Thanks!!

💖

@otagi
Copy link
Author

otagi commented Jul 27, 2016

I ended up doing something like this in the controller:

def edit
  @library = Library.find(params[:id])
  [MAX_BOOKS - @library.books.size, 0].max.times { @library.books.build }
  @library_form = LibraryForm.from_model(@library)
end

because I need to preserve and display the existing books.

My next issue is to make Rectify form work with accepts_nested_attributes_for :books in the model. I didn't find any combination that generates the correct HTML and saves the nested attributes on submit.

Did you already use nested attributes with Rectify?

@andypike
Copy link
Owner

andypike commented Aug 2, 2016

Hi @otagi,

Personally, I don't use accepts_nested_attributes_for anywhere with Rectify. As it constantly causes me pain (hence one of the reasons for creating Rectify). I typically make the assignments in a more explicit way and remove some of the "magic":

@form = LibraryForm.from_params(params)

@form.books.each do |book|
  library.books.create!(book.attributes)
end

So there is more code, but it's easier to understand what is going on and you don't need to juggle the HTML to match the format that Rails needs which always catches me out. How does that sound?

Thanks again for the question.

@otagi
Copy link
Author

otagi commented Aug 26, 2016

Thanks for the answer, @andypike.

The explicit assignment makes a lot of sense in this context. I like the idea. Less magic, but still easy to read.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants