13 Complex Joins in the Web Interface
Several edit pages have sections for editing associations that use multi-select boxes with huge lists in them. These are being replaced by a search box the will display a list of of entities from the associated table and offer the user the opportunity toggle the “linked” status of each displayed entity–that is, unlink linked entities and link unlinked entities.
Here is a list of files that need to be revised. We’ll call the primary table being edited “primary” and the associated table “associate”.
primary_controller.rb_edit_primary_associate.html.erbroutes.rb
These files need to be added:
_associate_tbody.html.erbsearch_associate.js.erbedit.js.erbadd_primary_associate.js.erbrem_primary_associate.js.erb
[Note: The convention for Rails join tables is to alphabetize the names, and this convention was also used in naming some of the templates and corresponding actions. To maintain this convention, some of the file names above should be altered if primary follows associate alphabetically. In this case, the file names to use will be edit_associate_primary.html.erb, add_associate_primary.js.erb, and rem_associate_primary.js.erb in place of the names given above, and the names of actions and references to these files in the files themselves will have to be adjusted accordingly.]
Here are step-by-step instructions for making the required changes. The changes in place for editing sites associated with a citation may serve as a model (see the controller file citations_controller.rb and the template files in app/views/citations; in the case of the template files, these can simply be copied and a search and replace done to replace the primary and associated table names (use the singular form when doing this):
- Revise the
_edit_primary_associate.html.erbpartial (copy e.g.app/views/citations/_edit_citations_sites.html.erband replacecitationandsitewith the appropriate strings). - Make the
_associate_tbody.html.erbpartial. - Alter the
routes.rbfile, adding a member route of the formget :search_associateto the resources forprimary. - Add a
search_associate.js.erbJavaScript template. - Add a search (
search_associate) action to the controller. - If needed, add a search scope to the model for
associate(or write the action in step 5 in a way so that one isn’t required). - At this point searching associates on the edit page should work. (If needed, add an instance variable for the associated collection to the
editaction.) - Add an
edit.js.erbJavascript template. - Alter the
editaction of the controller so that it handlesjsformat. (render layoutshould befalse, and additional template variables may have to be defined in the action.) Also, alter the update action to be ensure that if it renders the edit template on error, it has the variables it needs. - At this point the “Show only related …” that appears after doing a search should be functional.
- In
routes.rb, rename the:edit_primary_associateroute to:add_primary_associateand change the method fromposttoget. - Rename the corresponding action in the controller and revise this action (use
CitationsController#add_citations_sitesas a model). - Add the
add_primary_associate.js.erbJavaScript template. - At this point, linking an associated entity to the primary entity by clicking on the
+sign should work. - In the controller, revise the
rem_primary_associateaction (useCitationsController#rem_citations_sitesas a model). - Add the
rem_primary_associate.js.erbJavaScript template. - At this point, unlinking an associated entity to the primary entity by clicking on the
Xshould work. The “Update” link that appears next to the “Existing Sites Relationships” table caption after unlinking an associate should now work and should erase from view the entity you just unlinked.