# RESTful design and many forms in one view

**URL:** <https://www.ruby-forum.com/t/restful-design-and-many-forms-in-one-view/142834>\
**Category:** Rails\
**Created:** [July 11, 2008, 10:15pm UTC](https://www.ruby-forum.com/t/restful-design-and-many-forms-in-one-view/142834 "2008-07-11T22:15:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [July 11, 2008, 10:15pm UTC](https://www.ruby-forum.com/t/restful-design-and-many-forms-in-one-view/142834/1 "2008-07-11T22:15:22Z")

</div>

I have to admit I’m still wrapping my brain around not only MVS, but  
RESTful designs, so I’m sorry if this is a newbie question.

I have these models:

raid  
has\_many :raid\_drops  
has\_many :members, :through =\> :attendance  
has\_many :pickups

attendance  
belongs\_to :member  
belongs\_to :raid

member  
has\_many :attendances  
has\_many :raids, :through =\> :attendances  
has\_many :raid\_drops  
has\_many :bank\_activities

raid\_drop  
belongs\_to :raid  
belongs\_to :member  
belongs\_to :pickup

pickup  
belongs\_to :raid

Now, my first instinct told me I should have a path like these to get  
to a “raid\_drop”:

/members/1234/raid\_drops/92392  
/raids/1/raid\_drops/92392

Each will limit the search context to that specific member or raid,  
but I don’t actually know how to do this.

The larger problem is, I want one page (/raids/123123, the “show”  
action) to have a way to add more members, pickups, or drops from that  
page. I have forms working which will do some of this – I can add  
pickups and drops and so on, but I have the post from these forms send  
to the pickup controller, and that redirects back to the view for the  
raid. This is not perfect, and if I mix AJAX in, it becomes even less  
so.

Any advice?

---

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [July 15, 2008, 12:02pm UTC](https://www.ruby-forum.com/t/restful-design-and-many-forms-in-one-view/142834/2 "2008-07-15T12:02:56Z")

</div>

The associations look a bit tricky, eg. there is a relationship  
between member and raid, and member and raid\_drops, and also member  
and raid\_drops directly. I can only assume you have a reason for this  
slightly circular arrangement.

I have a few forms that handle more than one association at a time,  
and I actually find it easier using AJAX, because this can be managed  
in a way that allows a more REST approach. It is going to depend a  
bit on how you want the functionality, but the main parent seems to be  
the member. In which case, adding new members is likely to involve a  
full view update. adding pickup and drops could be done by using an  
ajax call. just make sure you have the correct parent id being  
included. (This can be either through hidden fields, or more often  
within the path in the remote links/form definitions).

For example, If you have a controller for pickups then you can have an  
index, update, (new), create, destroy and use resources routes. The  
simplest way to view this is to have an index partial in the views  
folder for the controller for that model or association. And then do  
the render update for that area, using the index partial for that  
model to replace that div or table etc. Remember to use the full  
path name for the partial, so that it picks it up from the right view  
folder.

In your case, because you have a number of interdependencies, each  
area being updated, it may involve updating other areas of the view  
too, but that can be done from within the render blocks by including  
whichever index partials you need.

Don’t know if any of that helps, but it is the approach I take and it  
generally works quite well. Once you have got the partials built, it  
also tends to be fairly easy to re-use them in various different  
views.

Tonypm

---

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [July 15, 2008, 7:16pm UTC](https://www.ruby-forum.com/t/restful-design-and-many-forms-in-one-view/142834/3 "2008-07-15T19:16:04Z")

</div>

I ended up doing something like this in the controller:

def index  
respond\_to do |format|  
format.html { # index.html.erb  
index\_load  
}  
format.js {  
field = params[:field] || “item\_name”  
case field  
when “item\_name”  
conditions = [‘item\_name ILIKE ?’, “%#{params[:search]}%”]  
@field = :item\_name  
when “disposition”  
conditions = [‘disposition ILIKE ?’, “#{params[:search]}%”]  
@field = :disposition  
end  
@items = RaidDrop.find(:all, :conditions =\> conditions)  
}  
format.xml {  
index\_load  
render :xml =\> @raid\_drops  
}  
end  
end

…  
protected  
def raid\_load  
if !params[:raid\_id].blank?  
@raid = Raid.find(params[:raid\_id])  
end  
end

def index\_load  
raid\_load  
if @raid  
@raid\_drops = @raid.raid\_drops.sort  
else  
@raid\_drops = RaidDrop.find(:all).sort  
end  
end

This allows me to access a raid\_drop and have a form where I can fill  
in the raid\_id, or have it accessed with a pre-filled-in one (that is,  
the paths are /raid\_drop/new or /raid/1234/raid\_drop/new)

I guess this is a “ploymorphic” controller. Works well so far 🙂

–Michael
