在html.erb文件中创建方法 [英] Create method in html.erb file
问题描述
我必须在文件 myCode.html.erb中创建一个方法。我必须用html编写ruby代码。到目前为止,我开始知道如何编写方法并按如下方式调用该方法。
I have to create one method in file say 'myCode.html.erb'. I have to write ruby code with html. So far I came to know how to write method and call that method as below.
method creation
<%def helo
print "This is function"
end%>
<%=helo%> #calling method
但是我在如何编写必须同时写两种方法的方法上感到困惑红宝石和html代码。以下是我需要在方法中编写的代码。
But I'm in dilemma about how to write method in which I have to write both ruby and html code. Below is my code which I need to write in method.
<%
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代码,则无法正常工作。它显示语法错误。
If I take the above created method helo
as a reference and write this ruby+html code in that manner, it is not working. It is showing syntax error.
推荐答案
在某些助手中定义方法
module TestHelper
def test
return 'Hello World'
end
end
在HTML文件/ .erb中,您只需插入
In your HTML file / .erb, you just insert
<%= TestHelper.test %>
在您的HTML中,您会看到 Hello World
In your HTML you will see 'Hello World'
这篇关于在html.erb文件中创建方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!