我正在开发一个应用程序,您可以在其中添加任务等。我知道这有点奇怪,但我只是想看看其他人如何实现它。您将如何将以下代码更改为辅助方法并使用它?
原始代码
<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 %>
我做辅助方法的方式
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
在我看来
<%= @work.task_check %>
最佳答案
就个人而言,我根本不会提取它,这是 View 逻辑,它属于 View 。它绝对不属于模型,但可以说可以提取到助手中。我会稍微改变它:
<h2>My Tasks</h2>
<% if @work.tasks.blank? %>
<%= link_to 'Create a new task', new_task_path %>
<% else %>
<%= render :partial => 'notes/note', :locals => {:note => @note} %>
<% end %>
调用 blank?
而不是 empty?
即使 @work.tasks
也能正常工作是nil
.
关于ruby-on-rails - 如何更改为查看辅助方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10290815/