Forum: Ruby on Rails Creating sub actions

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Dipesh B. (Guest)
on 2006-04-08 14:01
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?
Wiebe C. (Guest)
on 2006-04-08 14:31
(Received via mailing list)
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.
Anocha Yimsiriwattana (Guest)
on 2006-04-08 14:37
(Received via mailing list)
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'
Wiebe C. (Guest)
on 2006-04-08 14:52
(Received via mailing list)
<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 :)
Dipesh B. (Guest)
on 2006-04-08 17:23
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?
Brian H. (Guest)
on 2006-04-08 17:56
(Received via mailing list)
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.
This topic is locked and can not be replied to.