# Need help DRYing this code

**URL:** <https://www.ruby-forum.com/t/need-help-drying-this-code/156718>\
**Category:** Rails\
**Created:** [January 1, 2009, 10:33pm UTC](https://www.ruby-forum.com/t/need-help-drying-this-code/156718 "2009-01-01T22:33:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![devaulw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8e7dd6/32.png) [@devaulw](https://www.ruby-forum.com/u/devaulw)\
**Post date:** [January 1, 2009, 10:33pm UTC](https://www.ruby-forum.com/t/need-help-drying-this-code/156718/1 "2009-01-01T22:33:22Z")

</div>

I am new to this and need some help DRYing this view code:

### drop task name

Projects \<% for project in @projects %\> \<% project\_id = "project\_#{project.id}" %\> - \<%=h project.name %\> 

```
<% #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 %\>

Contexts \<% for context in @contexts %\> \<% context\_id = "context\_#{context.id}" %\> - \<%=h context.name %\> 
 \<%= 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 %\>

What is the best way to do this?

Thanks,  
Bill

---

<div class="post-metadata">

**Author:** ![devaulw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8e7dd6/32.png) [@devaulw](https://www.ruby-forum.com/u/devaulw)\
**Post date:** [January 2, 2009, 12:38pm UTC](https://www.ruby-forum.com/t/need-help-drying-this-code/156718/2 "2009-01-02T12:38:45Z")

</div>

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](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![devaulw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8e7dd6/32.png) [@devaulw](https://www.ruby-forum.com/u/devaulw)\
**Post date:** [January 2, 2009, 4:31pm UTC](https://www.ruby-forum.com/t/need-help-drying-this-code/156718/3 "2009-01-02T16:31:51Z")

</div>

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](mailto:removed_email_address@domain.invalid)

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

Here is what I’m using now:

### drop task name

Projects \<% for project in @projects %\> \<%= render :partial =\> "drop\_belongs\_to\_list", :locals =\> { :drop\_item =\> project } %\> \<% end %\>

Contexts \<% for context in @contexts %\> \<%= render :partial =\> "drop\_belongs\_to\_list", :locals =\> { :drop\_item =\> context } %\> \<% end %\>

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
