# Comment requested on first Rails code

**URL:** <https://www.ruby-forum.com/t/comment-requested-on-first-rails-code/124044>\
**Category:** Rails\
**Created:** [December 23, 2007, 11:14pm UTC](https://www.ruby-forum.com/t/comment-requested-on-first-rails-code/124044 "2007-12-23T23:14:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![janeden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@janeden](https://www.ruby-forum.com/u/janeden)\
**Post date:** [December 23, 2007, 11:14pm UTC](https://www.ruby-forum.com/t/comment-requested-on-first-rails-code/124044/1 "2007-12-23T23:14:02Z")

</div>

Hi,  
I recently started playing with RoR, and I am not sure if I got my setup  
right. Could someone comment on the code below?

Background: I want to expand/collapse a list of items. The expanded list  
represents a recursive hierarchical tree, while the collapsed one shows  
the  
first tree level below the current position.

1. My controller has:

def index

# This action defines the content of a complete page, irrelevant code

has  
been omitted)  
end

def expand

# irrelevant code omitted

render :partial =\> ‘children\_recursive’  
end

def collapse

# irrelevant code omitted

render :partial =\> ‘children’  
end

The relevant part of the index view looks like this:

\<%= render :partial =\> 'children' %\>

The children and children\_recursive partial views contain two lines  
each:

\<%= link\_to\_remote(“Expand”, :update =\> ‘children’, :url =\> { :action =\>  
“expand”, :id =\> @page.id })%\>  
\<%= display\_children %\>

# 

\<%= link\_to\_remote(“Collapse”, :update =\> ‘children’, :url =\> { :action  
=\>  
“compress”, :id =\> params[:id] })%\>  
\<%= display\_children(@children, true) %\>

The real work is done by a helper method (display\_children), which  
creates a  
recursive HTML list if the second parameter evaluates to true:

def display\_children(children=@children, recursive=nil)

# irrelevant code omitted

end

Coming from Perl, I know there’s more than one way to do it - but does  
this  
look like a reasonable way? I feel that there should be a simpler way  
than  
creating two actions, two partial views and a helper method for a simple  
task like this.

Thanks for your comments,  
Jan

---

<div class="post-metadata">

**Author:** ![janeden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@janeden](https://www.ruby-forum.com/u/janeden)\
**Post date:** [December 24, 2007, 12:20am UTC](https://www.ruby-forum.com/t/comment-requested-on-first-rails-code/124044/2 "2007-12-24T00:20:46Z")

</div>

#irrelevant suggestion omitted

Looks alright to me.

On Dec 24, 2007 8:43 AM, Jan E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def index
> 
> # irrelevant code omitted
> 
> Jan
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.
