Forum: Ruby on Rails Need help DRYing this code

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.
Bill D. (Guest)
on 2009-01-01 23:33
I am new to this and need some help DRYing this view code:

<div style="float: left;">
<h3>drop task name</h3>
<ul>Projects
<% for project in @projects %>
    <% project_id = "project_#{project.id}" %>
    <li id="<%= project_id %>" class="project" style="background-color:
#CCC; margin: 0 20px 2px 0;">
        <%=h project.name %>
    </li>

    <% #TODO need to DRY up the drop_receiving_element, perhaps with a
helper%>

    <%= drop_receiving_element(
        project_id,                           # The id of the receiving
element
        :accept   => "task_name",             # The CSS class of the
dropped element
        #The action to call (update the database and then update the
project name field for that task via RJS)
        :url      => { :controller => :tasks, :action => :update,
'task[project_id]'=> project.id.to_s }
        #TODO there is probably a better way to refer to the task's
project_id so that we can avoid the extra line in the update action of
the task controller
    ); %>
<% end %>
</ul>

<ul>Contexts
<% for context in @contexts %>
    <% context_id = "context_#{context.id}" %>
    <li id="<%= context_id %>" class="context" style="background-color:
#CCC; margin: 0 20px 2px 0;">
        <%=h context.name %>
    </li>
    <%= drop_receiving_element(
        context_id,                           # The id of the receiving
element
        :accept   => "task_name",             # The CSS class of the
dropped elememt
        #The action to call (update the database and then update the
project name field for that task via RJS)
        :url      => { :controller => :tasks, :action => :update,
'task[context_id]'=> context.id.to_s }
        #TODO there is probably a better way to refer to the task's
context_id so that we can avoid the extra line in the update action of
the task controller
    ); %>
<% end %>
</ul>
</div>

What is the best way to do this?

Thanks,
Bill
Andrius C. (Guest)
on 2009-01-02 13:38
(Received via mailing list)
1. You could create a partial with code between for and end, not
including them. Then u can render that partial using render :partial
=> 'something', :collection => @projects.
2. dom_id(project) is same as "project_#{project.id}"
3. Try content_tag_for(:li, project, ...) instead of <li id="<%=
project_id %>"...

You can read more about all of these methods in rails documentation
page.

On Jan 1, 11:33 pm, Bill D. <removed_email_address@domain.invalid>
Bill D. (Guest)
on 2009-01-02 17:31
Andrius C. wrote:
> 1. You could create a partial with code between for and end, not
> including them. Then u can render that partial using render :partial
> => 'something', :collection => @projects.
> 2. dom_id(project) is same as "project_#{project.id}"
> 3. Try content_tag_for(:li, project, ...) instead of <li id="<%=
> project_id %>"...
>
> You can read more about all of these methods in rails documentation
> page.
>
> On Jan 1, 11:33�pm, Bill D. <removed_email_address@domain.invalid>

Many thanks!  My Rails skills are improving by the day.

Here is what I'm using now:

<div style="float: left;">
<h3>drop task name</h3>
<ul>Projects
<% for project in @projects %>
  <%= render :partial => "drop_belongs_to_list", :locals => { :drop_item
=> project } %>
<% end %>
</ul>

<ul>Contexts
<% for context in @contexts %>
  <%= render :partial => "drop_belongs_to_list", :locals => { :drop_item
=> context } %>
<% end %>
</ul>
</div>

and the partial is:

<%= content_tag :li, (link_to (h drop_item.name), (drop_item)), :class
=> drop_item.class.to_s.downcase, :id=> dom_id(drop_item) %>

<%= drop_receiving_element(
    dom_id(drop_item),         # The id of the receiving element
    :accept   => "task_name",  # The CSS class of the dropped element
    #The action to call (update the database and then update the project
name field for that task via RJS)
    #Not RESTful yet
    :url      => { :controller => :tasks, :action => :update,
"task[#{drop_item.class.to_s.downcase}_id]" => drop_item.id.to_s }
); %>

Far more readable in the main view.  If I could only make the
drop_receiving_element RESTful, I'd be all set!

Bill
This topic is locked and can not be replied to.