开发者

Moving logic out of my view for a rails app

Okay, so basically I coded this app that does some calculations for each point of the section. That part of the code is handled within the controller so it's okay. The thing is that I need some of the calculations to show up on the screen so I can print(to a paper) whatever

    <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 %>

Here's some sample output http://img35.imageshack.us/i/screenshot20110325at133.png/ Full source code can be found over at http://github.com/carvefx/Roadie

How would I move (part) of this logic away from the view in the true spirit of rails. Syntax that looks weird is LaTeX, I need that for outputting math on the web.


There are several ways to move the code to other places. If it belongs to the controller, then you can move it to the controller code, probably making it a protected method.

If it is for helping to create the view, for some general methods, you can use helpers, such as application helpers. (or helper particular to that view)

If some logic belongs to the data in DB, then you can move it to model, which is to make it a "thin controller, fat model": http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜