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 %>
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.
dom_id(project) is same as “project_#{project.id}”
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.
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.
dom_id(project) is same as “project_#{project.id}”
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.
<%= 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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.