# Creating sub actions

**URL:** <https://www.ruby-forum.com/t/creating-sub-actions/57239>\
**Category:** Rails\
**Created:** [April 8, 2006, 12:01pm UTC](https://www.ruby-forum.com/t/creating-sub-actions/57239 "2006-04-08T12:01:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dipeshbatheja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dipeshbatheja](https://www.ruby-forum.com/u/dipeshbatheja)\
**Post date:** [April 8, 2006, 12:01pm UTC](https://www.ruby-forum.com/t/creating-sub-actions/57239/1 "2006-04-08T12:01:33Z")

</div>

Hi,  
So finally I have also decided to roll my engines on Rails. But I am  
kind of stuck in a situation. I understand that for every model there is  
a controller class. This controller class has cirtain set of methods  
where each method defines cirtain action to be performed. And each of  
this action renders cirtain UI.

[The background]  
Now let say I have a object “Car”. Apart from having basic attributes  
like car name, model, etc etc, tt has a list of “parts” that a car has.  
This essentially means i have two models (or tables in db) “Car” and  
“Part” and “Cars\_Controller” and “Parts\_COntroller”.

[The Problem]  
Now I want to develop an edit interface for this scenario. The edit  
interface has two tabs “General Car Details” and “Car Parts”.  
Now I understand that when I select to “edit” a car. This will take me  
two first edit screen where I will see “General Car Details” (as default  
selected tab). The problem is that I couldn’t figure out how do I move  
to next tab which is “Parts” tab. Should I create a new action something  
like “EditCarParts” or is there any way to define sub action for edit  
action or something liek that?

My second question is by default an ection is binded to a citan view.  
For example “Edit” action is binded to “Edit.rhtml” page. How can i  
override this?

---

<div class="post-metadata">

**Author:** ![dipeshbatheja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dipeshbatheja](https://www.ruby-forum.com/u/dipeshbatheja)\
**Post date:** [April 8, 2006, 12:31pm UTC](https://www.ruby-forum.com/t/creating-sub-actions/57239/2 "2006-04-08T12:31:34Z")

</div>

On Saturday 08 April 2006 12:01, Dipesh B. wrote:

> This essentially means i have two models (or tables in db) “Car” and  
> action or something liek that?
> 
> My second question is by default an ection is binded to a citan view.  
> For example “Edit” action is binded to “Edit.rhtml” page. How can i  
> override this?

Your first question can be answered by knowing that you can let a link  
call any  
controller or action. You can easily have a link in your car view, like  
so:

link\_to(“edit this part”, {:controller =\> “parts”, :action =\> “edit”,  
:id =\>  
part.id})

Also, you don’t need one controller for every model. You could easily  
have one  
controller for your car, with which you can edit parts as well. There  
_is_ one  
controller for every view.

In this case however, I would make a seperate controller for your parts,  
because I would imagine you want to edit parts individually. Editing the  
parts  
of a car would only involve assinging parts to cars, which should go in  
the  
view and controller of a car, because it is data of a car.

And about overriding the rhtml names, I don’t believe you can, nor can I  
think  
of why.

---

<div class="post-metadata">

**Author:** ![dipeshbatheja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dipeshbatheja](https://www.ruby-forum.com/u/dipeshbatheja)\
**Post date:** [April 8, 2006, 12:52pm UTC](https://www.ruby-forum.com/t/creating-sub-actions/57239/3 "2006-04-08T12:52:58Z")

</div>

\<posted & mailed\>

On Saturday 08 April 2006 08:35, Anocha Yimsiriwattana wrote:

> For the second question, using
> 
> render :action=\>‘name\_of\_rhtml’

Oh yeah, I forgot 🙂

---

<div class="post-metadata">

**Author:** ![dipeshbatheja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dipeshbatheja](https://www.ruby-forum.com/u/dipeshbatheja)\
**Post date:** [April 8, 2006, 12:37pm UTC](https://www.ruby-forum.com/t/creating-sub-actions/57239/4 "2006-04-08T12:37:39Z")

</div>

Dipesh B. wrote:

> This essentially means i have two models (or tables in db) “Car” and  
> action or something liek that?
> 
> My second question is by default an ection is binded to a citan view.  
> For example “Edit” action is binded to “Edit.rhtml” page. How can i  
> override this?

For the second question, using

render :action=\>‘name\_of\_rhtml’

---

<div class="post-metadata">

**Author:** ![dipeshbatheja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dipeshbatheja](https://www.ruby-forum.com/u/dipeshbatheja)\
**Post date:** [April 8, 2006, 3:56pm UTC](https://www.ruby-forum.com/t/creating-sub-actions/57239/5 "2006-04-08T15:56:53Z")

</div>

I suggest taking a look at routes. You can make your URLs look however  
you  
want with those. You can also use modules to group your models in  
folders.  
You can use routes and modules together to make your urls more pleasing.

so instead of

/cars/  
/parts/

you could have

/car/  
/car/parts

and if you really want to put the effort in, you can do

/car/5/parts

But you need t o read the stuff that’s out there to really understand it  
all. Start with a search of the wiki and read through the agile book. I  
am  
confident that if you know Rails well enough you can pretty much do  
anything  
with it.

---

<div class="post-metadata">

**Author:** ![dipeshbatheja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dipeshbatheja](https://www.ruby-forum.com/u/dipeshbatheja)\
**Post date:** [April 8, 2006, 3:23pm UTC](https://www.ruby-forum.com/t/creating-sub-actions/57239/6 "2006-04-08T15:23:06Z")

</div>

Wiebe C. wrote:

> \<posted & mailed\>
> 
> On Saturday 08 April 2006 08:35, Anocha Yimsiriwattana wrote:
> 
> > For the second question, using
> > 
> > render :action=\>‘name\_of\_rhtml’
> 
> Oh yeah, I forgot 🙂

Thank you Anocha and Wiebe, but still have doubts in first question.  
Firstly i just want to display list of all the parts associated with a  
car when i edit that car’s details. Now, the url to edit the car could  
look something like /CarApp/Cars/edit/5, which means i am editing the  
car number 5. Can’t we have something like /CarApp/Cars/edit/5/parts  
which will mean I am seeing list of all the parts associated with car  
number 5 in edit mode. How do i organise my code to get this kind of  
URL?
