3

I have a collection of records that are related to a specific parent object. I have no need to update the parent, just want to update all the children.

I tried making a Reform::Form and simply adding the collection declaration. An example might be a developer has many projects. When the developer goes on holiday his projects are "on_hold", but some of them might still be active. So, I would like to list all the projects with a checkbox to check if they should be put on hold. I essentially want to have a simple_fields_for :projects.

Does it make sense to use Reform for this?

I have this code:

class ProjectsForm < Reform::Form

    collection :projects do
      property :name  #perhaps I want to rename them in the same form (bear with me)
      property :on_hold
    end
end

This should work, but when initializing the form with a hash

@form = ProjectsForm.new({projects: @array_of_projects})

I get an error

undefined method `projects' for #<Hash:0x007fce8f2783b8>

As if the collection method is not working. I am obviously doing something stupid.

I'd really like to use Reform. I love the philosophy behind a lot of the trailblazing suit of gems. It will be great if someone can point me in the right direction here.

If it turns out this isn't a good usecase for Reform I'll find another :P

Update:

I think reform is slightly more coupled with the idea of a model than what I thought. I thought it was just a form object with properties to play nicely with form builder. I now find that your model is key. You need to initialize the Reform form with A model, or in the case of composition, a few models. However, if you pass in an array of hash reform believes this is the model and then tries to access the :projects property on the model. In my case a hash.

I have now added a

# my contrived example is getting lame
attr_accessor :projects 

to the developer class and it is working. At least the generating the reform object is working.

I am still curious wether this is a good use-case for Reform.

Apie
  • 6,371
  • 7
  • 27
  • 25
  • Try passing as an array `ProjectForm.new([{projects: @array_of_projects}])` – Sonalkumar sute Mar 19 '15 at 20:21
  • Same problem - just undefined method on Array. – Apie Mar 20 '15 at 04:02
  • You should convert `@array_of_projects` to `key` `value` pair like `array_of_hashes = [{project: project1}, {project: project2}, {project: project3}]` and then pass to your query `@form = ProjectsForm.new(array_of_hashes)` – Sonalkumar sute Mar 20 '15 at 05:52

0 Answers0