Create method in html.erb file

微笑、不失礼 提交于 2019-12-10 17:54:26

问题


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

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>  
<% } %>

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.


回答1:


Use helper method . Use rails DRY principle.

Just write Method in ApplicationHelper Module.

Example:

module ApplicationHelper
  def halo
    "This is function" 
   end
 end



回答2:


It is not likely what you really want to do. But you can do it.

<% 
def hello
  'Hello World'
end
%>

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



回答3:


Define method in some Helper

module TestHelper
  def test
    return 'Hello World'
  end
end

In your HTML file / .erb, you just insert

<%= TestHelper.test %>

In your HTML you will see 'Hello World'




回答4:


A correct way to do that is to define a partial that can consume parameters.

To achieve that, you render your partial from the calling view with:

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

In the partial, you just render the value of the variables where you need them with:

<%= variable %>

For example,

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


来源:https://stackoverflow.com/questions/29839991/create-method-in-html-erb-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!