将 Rails 应用程序的逻辑移出我的视野
好的,基本上我编写了这个应用程序,该应用程序对该部分的每个点进行一些计算。这部分代码是在控制器内处理的,所以没问题。问题是我需要一些计算结果显示在屏幕上,以便我可以打印(到纸上)任何内容
<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 找到
我将如何移动(部分)这种逻辑背离了真正的 Rails 精神中的观点。看起来很奇怪的语法是 LaTeX,我需要它来在网络上输出数学。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有多种方法可以将代码移动到其他位置。如果它属于控制器,那么您可以将其移至控制器代码,可能使其成为受保护的方法。
如果是为了帮助创建视图,对于一些通用方法,可以使用helpers,例如应用程序helpers。 (或特定于该视图的帮助器)
如果某些逻辑属于数据库中的数据,那么您可以将其移动到模型,这就是使其成为“瘦控制器,胖模型”: http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model
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