How can I simulate editing multiple related resources on the same web page?

Let's say we have a company model with many employees and many projects.

If we want to show projects, we will go to "/ Company / 1 / projects / index"

If we want to edit 1 project, we will go to "/ Company / 1 / projects / 1 / edit"

What if we want to edit all projects at once on the same web page?
We can go to "/ company / 1 / edit" and put nested forms for all projects

But what if we need a different web page to edit all employees at the same time?
We cannot use "/ company / 1 / edit" again.

We are currently doing "/ company / 1 / projects / multiedit", "/ company / 1 / projects / multupdate", but as you can see, it doesn't fit.

How can we model this?

+2


source to share


3 answers


Resources do not control interface design


How you organize your domain model shouldn't be a major factor in GUI design and vice versa.

What if we want to edit all projects at the same time on the same web page?

Then you should probably provide widgets for each and POST each update in sequence. You can use AJAX to make this a pleasant experience for the end user.

We are currently doing "/ company / 1 / projects / multiedit", "/ company / 1 / projects / multupdate", but as you can see, it doesn't fit.

And that's fine if you don't need to be RESTful - right? Is your application for external use or for an internal business process?

There's a lot more REST in there than organizing resources - nested or otherwise. You should also be interested in navigating resource views.

If you really feel like you need to be RESTful and if you believe (as you mentioned in the comment below) that the state of all projects and employees should be atomically updated, then you should

1. Implement new container resources "Employees" and "Projects" to simulate the relationship between the Company and the "Employee" set and between the Company and the "Project" set.

2. In response to the GET for the company, you must include the URIs for the Employees and Projects resources (ie, two URIs in total).

3. In response to a GET on Employees or Projects, you must either return the state of all core resources or a URI for each so that their state can be determined.

4. When updating Employees, you have to submit all state of the underlying resources (presumably to one huge one). The new state completely replaces the old state,

This last step - a lot of overhead - you have to rethink the all-or-nothing constraint. Remember, this has nothing to do with REST - what you did is an invariant in your business logic for the service interface .




Personally, I would:

1.do your best to remove this invariant from the presentation layer

2.model resources in a non-nested way - it's more flexible and REST has nothing to do with URIs, except that each resource must have one

3.Present the resources of employees and projects to simulate the relationship between the company and the employee and the project

4. have a company view, return URIs for projects and for employees (two more).

5. Provide each employee and project representative with the appropriate company

6.Create a UI so that it can display the list of projects / employees for the company and allow everyone to update individually

7.package all POST together and send them via AJAX on the shared button click


It's worth taking a look at the excellent screencasts Ryan Bates has done on nested resources, but remember that nested resources are not a core part of REST - to quote Roy Fielding

The important thing is that every important resource has a URI that allows GET representations of that resource.

Enough said - good luck! Chris

+3


source


0


source


I think the way you are doing it now is pretty good. Inventing artificial resources just to fit into the basic REST URL scheme doesn't seem good to me. In addition, Rails has member collection and routes for the very purpose of adding additional actions, so what you do follows the Rails philosophy.

For me, defining resources and actions is similar to designing classes. Sometimes you add multiple methods and realize that the design would be better if you split the class in two, but other times the class just needs to have all of those methods.

Do you have a bona fide resource other than companies and projects that need to handle multiedit and multupdate? I think no. But again, as with classes, sometimes it's a matter of opinion.

0


source







All Articles