Forum: Ruby on Rails Best way to DRY this?

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.
Vincent F. (Guest)
on 2007-05-10 05:58
(Received via mailing list)
I am super new to rails and I was wondering how could I DRY this


<ul>
    <%- for page in @pages -%>
        <li><%= link_to_remote page.title, :url => { :action =>
"view_page", :id => page.id} %>
                <%= link_to_remote (image_tag('add')), :url => { :action
=> 'add_sub', :id => page.id } %>
                                 </li>
        <%- if page.children -%>
            <ul>
            <%- for subpage in page.children -%>
                <li id="page_<%= subpage.id %>">
                    <%= link_to_remote subpage.title, :url => { :action
=> "view_page", :id => subpage.id} %>
                    <%= link_to_remote (image_tag('delete')), :url => {
:action => 'delete_page',    :id => subpage.id } %></li>
            <%- end -%>
            </ul>
        <%- end -%>
    <%- end -%>
</ul>
Terry D. (Guest)
on 2007-05-10 07:13
(Received via mailing list)
Vincent, maybe I'm being dense here, but I'm not sure where you're
repeating
yourself.

On 5/9/07, Vincent F. <removed_email_address@domain.invalid> wrote:
> => 'add_sub', :id => page.id } %>
>             </ul>
>         <%- end -%>
>     <%- end -%>
> </ul>
>
>
> >
>


--
Terry (TAD) Donaghe
http://tadspot.tumblr.com
Vincent F. (Guest)
on 2007-05-10 08:08
(Received via mailing list)
Terry, Sorry I used the wrong wording. I think what I meant to say was I
seem to have alot of logic in my view I would like a more elegant way to
achieve what I have here. a helper? im am not sure.
Terry D. (Guest)
on 2007-05-10 20:01
(Received via mailing list)
Vincent, while I agree that you have some logic in your view, I'm not
sure
that it's too much.  I don't see where you'd need to pare something
down,
but maybe someone else will.  It looks fine to me.

On 5/9/07, Vincent F. <removed_email_address@domain.invalid> wrote:
> > On 5/9/07, *Vincent F.* < removed_email_address@domain.invalid
> >                     <%= link_to_remote (image_tag('add')), :url => {
> >                         <%= link_to_remote (image_tag('delete')), :url
> >
> >
>


--
Terry (TAD) Donaghe
http://tadspot.tumblr.com
Jeff C. (Guest)
on 2007-05-10 20:22
(Received via mailing list)
On May 9, 8:56 pm, Vincent F. <removed_email_address@domain.invalid> wrote:
>             <ul>
> </ul>
Looks pretty good, but if things feel ugly, you might  moving the
inside of each loop intto partials:

So your whole template is reduced to:

<ul>
  <%= render :partial => 'page', :collection => @pages %>
</ul>

and then _page.rhtml would be:

<li><%= link_to_remote page.title, :url => { :action =>
"view_page", :id => page.id} %>
      <%= link_to_remote (image_tag('add')), :url => { :action =>
'add_sub', :id => page.id } %>
</li>

<%= render :partial => 'subpage', :collection => page.children if
page.children %>

and finally _subpage.rhtml would be:

<li id="page_<%= subpage.id %>">
   <%= link_to_remote subpage.title, :url => { :action =>
"view_page", :id => subpage.id} %>
   <%= link_to_remote (image_tag('delete')), :url => {:action =>
'delete_page',    :id => subpage.id } %>
</li>


More files, but each one is simple.  Just food for thought.

Jeff
softiesonrails.com
This topic is locked and can not be replied to.