html - 在 html.erb 文件中创建方法

标签 html ruby erb

我必须在文件中创建一个方法,比如“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 代码,它是行不通的。它显示语法错误。

最佳答案

这不太可能是您真正想做的。但你可以做到。

<% 
def hello
  'Hello World'
end
%>

<p>This is simply an erb file (really just a text file)</p>
<p><%= hello %></p>

关于html - 在 html.erb 文件中创建方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29839991/

相关文章:

css - 将 style.css.scss 更改为 style.css.scss.erb 时找不到文件错误

html - Rails 在 <%= 插入之前忽略空格

html - 使用 HTML & CSS3 : How to extend the borders of the inner table vertically, 使其触及外表

javascript - Link on Link - 如何在没有父链接激活的情况下激活 JS 代码?

css - 在 scss 文件中使用 config.rb 中定义的变量

ruby-on-rails - 此括号语法的 Ruby 2.4 文档

html - 为什么 Rails 给我 "Can' t verify CSRF token authenticity”错误?

css 中的 PHP 变量不更新/刷新

html - 如何使用CSS隐藏表格的第二行?

ruby-on-rails - 如何在 Ruby on Rails 中注册服务 worker ?