【问题标题】:Create method in html.erb file在 html.erb 文件中创建方法
【发布时间】: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 没有太大关系,但可能只是语法。您未显示的错误消息中可能表明了某些内容。

标签: html ruby erb


【解决方案1】:

使用辅助方法。使用 rails DRY 原则。

只需在 ApplicationHelper 模块中编写方法即可。

例子:

module ApplicationHelper
  def halo
    "This is function" 
   end
 end
【解决方案2】:

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

<% 
def hello
  'Hello World'
end
%>

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

【讨论】:

    【解决方案3】:

    在一些 Helper 中定义方法

    module TestHelper
      def test
        return 'Hello World'
      end
    end
    

    在您的 HTML 文件/.erb 中,您只需插入

    <%= TestHelper.test %>
    

    在您的 HTML 中,您将看到“Hello World”

    【讨论】:

      【解决方案4】:

      正确的做法是定义一个可以使用参数的部分。

      要实现这一点,您可以使用以下命令从调用视图中渲染您的局部视图:

      <%= render partial: "my_partial", locals: {variable: "my value", other_variable: "other value"} %>
      

      在部分中,您只需在需要它们的地方渲染变量的值:

      <%= variable %>
      

      例如,

      <span>Variable has value <%= variable %>, while the other variable has value <%= other_variable %>.</span>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-11
        • 2013-03-21
        • 1970-01-01
        • 2023-04-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多