Need help DRYing this code

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

  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. [email protected]

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. [email protected]

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