Consolidated question on rails with a form based on existing resources

You have a set of related models created using a scaffold, for example. a house that has many rooms, each of which has many windows, each of which has a choice of castles.
These resources are already filled with data, that is, someone entered all the information, for example: in a room called "kitchen" there are various windows associated with it, and these windows have five different locks associated with them.

Someone comes and says:

Can you create a form that allows someone to create a new project, where they can select different rooms, windows, and then specify the locks that they would like for this project? (they are already in the system, nothing new to add, just associations with a new project)

It sounds like a nested form, but I spent a lot of time solving it - there are many levels of nesting that make it difficult. Any suggestions?

+4
source share
3 answers

session based solution

With such deeply nested models, selecting a box at the front end will not be enough ... Assuming this, you might want to create a current_house that id lives in the session (how current_user works).

After your current_house will add different items by going through the list of items list and clicking the add_to link:

# house_controller.rb def add_to current_house.polymorphic_items << Kitchen.find(params[:id]) redirect_to :back end 

But there are many approaches to this session-based solution that implements a cart / order system. You can add current_item to add material to each leaf of your tree in the room of your house.

EG after clicking on the newly added kitchen:

 before_filter :set_current_item def add_to current_item.windows << Window.find(id) end 

current_item beeing polymorphic: living room, bathroom, etc. But how you implement this, it depends on your domain model ....

As a rule, with regard to nested forms, I will follow the directions of the guides for the routes: do not go down a single level, otherwise you will be in a mess.

+1
source

Yes, this is a nested form. Nested Railscasts are a great place to start.

If everything is already in the system, you probably just want to select the rectangles so that they can choose what they want. Also check out the .build method. If you have several levels of nesting, you can also manually set the association by transferring the foreign key yourself.

0
source

I think you can model this using one level of nested attributes, given the models below (based on pre-existing Windows / Locks and a room that just requires mixing and matching them with a set of windows with these locks):

 class House < ActiveRecord::Base has_many :rooms end class Room < ActiveRecord::Base belongs_to :house has_many :window_configs end class WindowConfig < ActiveRecord::Base belongs_to :room belongs_to :window belongs_to :lock end class Lock < ActiveRecord::Base has_many :window_configs end class Window < ActiveRecord::Base has_many :window_configs end 

... based on this model setting, you can create a single house shape in which you dynamically add child room definitions, each of which has a name and a collection of windows_configs that have two rectangles for each of them (select the window definition and then the definition blocking). Since you are dynamically adding multiple rooms with multiple windows, you will need JS to populate the new form elements, but they can all live in one sub-form.

 form_for :house do |form| # Dynamically add a Room form for each room you want with js form.fields_for :room do |room_attributes| room_attributes.text_field :name # Dynamically add window_config forms on Room w/ JS room_attributes.fields_for :window_config do |window_attributes| window_attributes.select :window_id, Window.all window_attributes.select :lock_id, Lock.all 
0
source

All Articles