将逻辑从我的视图移出rails应用程序

时间:2011-03-25 11:35:15

标签: ruby-on-rails view logic

好的,基本上我编写了这个应用程序,为该部分的每个点进行一些计算。代码的那部分是在控制器内处理的,所以没关系。问题是我需要一些计算显示在屏幕上,这样我就可以打印(到纸上)

    <h2>Output for error calculations</h2>

<%
@sections.each_with_index do |section, sindex|

    # Retrieve our values
    total_distance = @total_distances[sindex]
    @total_points_calc = @total_points_sections[sindex]
    total_points = section.points.count

%>
    <h3>Section <i><%= section.name %></i></h3>
    <hr>
    <p>\( \Sigma d_{<%= section.name %>} = <%= section.name %> \)</p>
    <p>
        \( \Sigma d_{<%= section.name %>}= \)
        <% 
        section.points.each_with_index do |point, index| %>
                <%=                 
                 # if this isn't the first section and the point.distance is 0
         sindex != 0 and point.distance == 0 ? point.distance = nil : point.distance = point.distance

                 # add a + after each point that isn't the last
                 index != @total_points_calc ? point.distance.to_s + ' +' : point.distance
                %>
        <% end %>
        = <%= total_distance%>
    </p>
    <p>
    \( <%= section.name %>= \) <%= section.length %>
    </p>
    <p>e = \( \frac{<%= total_distance %> - <%= section.length %>}{<%= @total_points_calc %>} \) = <%= @errors[sindex] %></p>
<% end %>

以下是一些示例输出http://img35.imageshack.us/i/screenshot20110325at133.png/ 完整的源代码可以在http://github.com/carvefx/Roadie

找到

我如何将这种逻辑的(部分)从真正的轨道精神中移开。看起来很奇怪的语法是LaTeX,我需要它来在网上输出数学。

1 个答案:

答案 0 :(得分:0)

有几种方法可以将代码移动到其他地方。如果它属于控制器,那么您可以将其移动到控制器代码,可能使其成为受保护的方法。

如果是帮助创建视图,对于某些常规方法,您可以使用帮助程序,例如应用程序帮助程序。 (或特定于该观点的帮助者)

如果某个逻辑属于DB中的数据,那么您可以将其移动到模型,这将使​​其成为“瘦控制器,胖模型”:http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model