【发布时间】:2015-04-24 06:14:26
【问题描述】:
我必须在文件“myCode.html.erb”中创建一种方法。我必须用 html 编写 ruby 代码。到目前为止,我知道如何编写方法并调用该方法,如下所示。
method creation
<%def helo
print "This is function"
end%>
<%=helo%> #calling method
但是对于如何编写必须同时编写 ruby 和 html 代码的方法,我处于两难境地。以下是我需要在方法中编写的代码。
<%
actions.each{ |action|
tc = []
bizA = []
testcaseName = ''
bizActionName = ''
@factory.testcases.rows({'steps.action._actionId' => action["_id"]}).each{|testcase|
d = ''
testcaseName = testcase['attributes']['name'] + d
d = ', '
tc << testcaseName
}
# require 'ruby-debug' ; debugger
if !action['isGrouping']
actions.each{|act|
if act['isGrouping']
temp = []
temp = act['steps']
temp.each{|step|
if action['_id']==step['action']['_actionId']
bizA << act['name']
end
}
end
}
end
%>
<tr>
<td><%= name %></td>
<td><%= action['name'] %></td>
<td><%= @factory.testcases.rows({'steps.action._actionId' => action["_id"]}).length %> </td>
<td>
<% counter=1%>
<% for ix in 0..tc.length-1 %>
<% if counter%2==0 %>
<%if ix!=tc.length-1 %>
<font color="black"><%= tc[ix] %></font> <br/>
<%else%>
<font color="black"><%= tc[ix] %></font> <br/>
<%end%>
<% else %>
<%if ix!=tc.length-1 %>
<font color="brown"><%= tc[ix] %></font> <br/>
<%else%>
<font color="brown"><%= tc[ix] %></font> <br/>
<%end%>
<%end%>
<% counter=counter+1 end %>
</td>
<td><%=bizA.length%></td>
<td>
<% counter=1%>
<% for ix in 0..bizA.length-1 %>
<% if counter%2==0 %>
<%if ix!=bizA.length-1 %>
<font color="black"><%= bizA[ix] %></font> <br/>
<%else%>
<font color="black"><%= bizA[ix] %></font> <br/>
<%end%>
<% else %>
<%if ix!=bizA.length-1 %>
<font color="brown"><%= bizA[ix] %></font> <br/>
<%else%>
<font color="brown"><%= bizA[ix] %></font> <br/>
<%end%>
<%end%>
<% counter=counter+1 end %>
</td>
</tr>
<% } %>
如果我以上面创建的方法helo 作为参考并以这种方式编写此 ruby+html 代码,则它不起作用。它显示语法错误。
【问题讨论】:
-
请不要。在视图中定义方法是非常糟糕的,不应该实践。改为定义助手。
-
@MarekLipka... 感谢您的回复。我是这种方式的新手。您能否更具体一点或给我一些示例链接,以便对您有所帮助。
-
您说“显示语法错误”。但是您没有显示语法错误。我猜它与方法或 HTML 没有太大关系,但可能只是语法。您未显示的错误消息中可能表明了某些内容。