Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 视图中case语句的Rails约定_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 视图中case语句的Rails约定

Ruby on rails 视图中case语句的Rails约定,ruby-on-rails,ruby,Ruby On Rails,Ruby,我不熟悉ruby和rails,所以我想问一个关于约定的问题 我有一个视图,它在一个表中生成一个项目列表,我被要求进行更改,在这样做的过程中,我在视图中添加了一个case语句,我认为这不是正确的操作方式,因此我认为我会仔细检查 我所做的更改只是根据上一个表列的值向tr添加一个类 list.rhtml <table width="100%"> <tr> <th style="width: 80px;">ID #</th>

我不熟悉ruby和rails,所以我想问一个关于约定的问题

我有一个视图,它在一个表中生成一个项目列表,我被要求进行更改,在这样做的过程中,我在视图中添加了一个case语句,我认为这不是正确的操作方式,因此我认为我会仔细检查

我所做的更改只是根据上一个表列的值向
tr
添加一个类

list.rhtml

<table width="100%">
    <tr>
        <th style="width: 80px;">ID #</th>
        <th>Organisation</th>
        <th>Product</th>
        <th>Carrier</th>
        <th>Carrier Ref</th>
        <th>Post Code</th>
        <th>Status</th>
    </tr>

    <%= render :partial => 'circuit/list_item', :collection => @circuits %>

</table>
<%
# code I have added
@tr_class = ''

case list_item.status
when 'Handover'
  @tr_class = ''
when 'Unprocessed'
  @tr_class = 'high_priority'
when 'Ceased'
  @tr_class = 'low_priority'
else
  @tr_class = ''
end
# end of newly added code
%>

<!-- the class part is new aswell -->
<tr class="<%= @tr_class %>">
    <td><a href='/circuit/update/<%= list_item.id %>'><%= list_item.id_padded %></a></td>
    <td><%= list_item.organisation.name if list_item.has_organisation? %></td>
    <td><%= list_item.product_name %></td>
    <td><%= list_item.carrier.name %></td>
    <td><%= list_item.carrier_reference %></td>
    <td><%= list_item.b_end_postcode %></td>
    <td><%= list_item.status %></td>
</tr>
<tr class="<%= tr_class_for_status(list_item.status) %>">

身份证#
组织
产品
载体
承运人参考号
邮政编码
地位
“电路/列表项目”:集合=>@circuits%>
列出项目。rhtml

<table width="100%">
    <tr>
        <th style="width: 80px;">ID #</th>
        <th>Organisation</th>
        <th>Product</th>
        <th>Carrier</th>
        <th>Carrier Ref</th>
        <th>Post Code</th>
        <th>Status</th>
    </tr>

    <%= render :partial => 'circuit/list_item', :collection => @circuits %>

</table>
<%
# code I have added
@tr_class = ''

case list_item.status
when 'Handover'
  @tr_class = ''
when 'Unprocessed'
  @tr_class = 'high_priority'
when 'Ceased'
  @tr_class = 'low_priority'
else
  @tr_class = ''
end
# end of newly added code
%>

<!-- the class part is new aswell -->
<tr class="<%= @tr_class %>">
    <td><a href='/circuit/update/<%= list_item.id %>'><%= list_item.id_padded %></a></td>
    <td><%= list_item.organisation.name if list_item.has_organisation? %></td>
    <td><%= list_item.product_name %></td>
    <td><%= list_item.carrier.name %></td>
    <td><%= list_item.carrier_reference %></td>
    <td><%= list_item.b_end_postcode %></td>
    <td><%= list_item.status %></td>
</tr>
<tr class="<%= tr_class_for_status(list_item.status) %>">


是否有一种Rails模式或约定可以使case语句脱离此视图?

如果正确理解您的问题,我认为您应该将
case
语句放在helper函数中:

app/helpers/list_helper.rb

module ListHelper
  def tr_class_for_status(status)
    case status
    when 'Unprocessed'
      'high_priority'
    when 'Ceased'
      'low_priority'
    else
      ''
    end
  end
end
\u列表\u项目。rhtml

<table width="100%">
    <tr>
        <th style="width: 80px;">ID #</th>
        <th>Organisation</th>
        <th>Product</th>
        <th>Carrier</th>
        <th>Carrier Ref</th>
        <th>Post Code</th>
        <th>Status</th>
    </tr>

    <%= render :partial => 'circuit/list_item', :collection => @circuits %>

</table>
<%
# code I have added
@tr_class = ''

case list_item.status
when 'Handover'
  @tr_class = ''
when 'Unprocessed'
  @tr_class = 'high_priority'
when 'Ceased'
  @tr_class = 'low_priority'
else
  @tr_class = ''
end
# end of newly added code
%>

<!-- the class part is new aswell -->
<tr class="<%= @tr_class %>">
    <td><a href='/circuit/update/<%= list_item.id %>'><%= list_item.id_padded %></a></td>
    <td><%= list_item.organisation.name if list_item.has_organisation? %></td>
    <td><%= list_item.product_name %></td>
    <td><%= list_item.carrier.name %></td>
    <td><%= list_item.carrier_reference %></td>
    <td><%= list_item.b_end_postcode %></td>
    <td><%= list_item.status %></td>
</tr>
<tr class="<%= tr_class_for_status(list_item.status) %>">

如果正确理解您的问题,我认为您应该将
case
语句放在helper函数中:

app/helpers/list_helper.rb

module ListHelper
  def tr_class_for_status(status)
    case status
    when 'Unprocessed'
      'high_priority'
    when 'Ceased'
      'low_priority'
    else
      ''
    end
  end
end
\u列表\u项目。rhtml

<table width="100%">
    <tr>
        <th style="width: 80px;">ID #</th>
        <th>Organisation</th>
        <th>Product</th>
        <th>Carrier</th>
        <th>Carrier Ref</th>
        <th>Post Code</th>
        <th>Status</th>
    </tr>

    <%= render :partial => 'circuit/list_item', :collection => @circuits %>

</table>
<%
# code I have added
@tr_class = ''

case list_item.status
when 'Handover'
  @tr_class = ''
when 'Unprocessed'
  @tr_class = 'high_priority'
when 'Ceased'
  @tr_class = 'low_priority'
else
  @tr_class = ''
end
# end of newly added code
%>

<!-- the class part is new aswell -->
<tr class="<%= @tr_class %>">
    <td><a href='/circuit/update/<%= list_item.id %>'><%= list_item.id_padded %></a></td>
    <td><%= list_item.organisation.name if list_item.has_organisation? %></td>
    <td><%= list_item.product_name %></td>
    <td><%= list_item.carrier.name %></td>
    <td><%= list_item.carrier_reference %></td>
    <td><%= list_item.b_end_postcode %></td>
    <td><%= list_item.status %></td>
</tr>
<tr class="<%= tr_class_for_status(list_item.status) %>">