简体   繁体   中英

Create method in html.erb file

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.

Use helper method . Use rails DRY principle.

Just write Method in ApplicationHelper Module.

Example:

module ApplicationHelper
  def halo
    "This is function" 
   end
 end

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>

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'

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM