I am working on a app where you can add task etc. I know this is kind of weird but I just like to see how others would implement this. How would you change the following code into a helper method and use it?
The original code
<h2>My Tasks</h2>
<% task = @work.tasks %>
<% if task.empty? %>
<%= link_to 'Create a new task', new_task_path %>
<% else %>
<%= render :partial => 'notes/note', :locals => {:note => @note} %>
<% end %>
My way of doing a helper method
def task_check
task = @work.tasks
if task.empty?
link_to 'Create a new task', new_task_path
else
render :partial => 'notes/note', :locals => {:note => @note}
end
end
In my view
<%= @work.task_check %>
Personally, I wouldn’t extract this out at all, this is view logic and it belongs in the views. It definitely doesn’t belong in a model, but it could arguably be extracted into a helper. I’d change it slightly:
Calling
blank?instead ofempty?will work even if@work.tasksisnil.