条件标记在视图中。为什么不好呢?[英]Conditional tags inside the view. Why bad? 本文翻译自  Mike Arriaga  查看原文  2014-07-22  450    ruby-on-rails/

时间:2022-11-03 09:53:45

In my rails app I have a very dynamic user dashboard that shows and hides div elements based on the users role (customer, employee, admin).

在我的rails应用程序中,我有一个非常动态的用户仪表板,根据用户角色(客户、员工、管理员)显示和隐藏div元素。

For example: users/show.html.erb

例如:用户/ show.html.erb

 <% if current_user.role_id == 4 %>
    <th>Edit</th>
    <th>Delete</th>
    <% elsif current_user.role_id == 1 %>
    <th>Cancel Appointment</th>
    <% else %>
    <% end %>
    </tr>
    </thead>

I stored the table nav tabs inside their own partials like this:

我把表格nav标签放在他们自己的部分里,就像这样:

<% if current_user.role_id == 1 %>
<%= render 'users/customer_bar' %>
<% elsif current_user.role_id == 2 %>
<%= render 'users/employee_bar' %>
<% elsif current_user.role_id == 4 %>
<%= render 'users/honcho_bar' %>

The same could be done for the aforementioned tags but that would not at all be DRY. Is there a better way? Why is it bad to conditionally format tags in this way?

对上述标签也可以这样做,但这一点也不枯燥。有更好的方法吗?为什么用这种方式有条件地格式化标签是不好的?

1 个解决方案

#1


2  

Statement

声明

Maybe you're looking for case / switch:

也许你在寻找箱子/开关:

case current_user.role_id
when 4
  #do something for 4 
when 1
  #do something for 1
end

--

- - -

System

系统

I would highly recommend using a partial to get this to work:

我强烈建议使用一个部分来让它工作:

<%= render partial: "your_partial", local: { your_local_var: "value" } %>

This will allow you to define the conditions in the partial itself:

这将使您能够定义部分本身的条件:

#app/views/controller/your_partial.html.erb
<% if var == "value" %>
  ...
<% else %>
  ...
<% end %>

Bad?

坏的?

I wouldn't say it's "bad" for the sake of it - it's typically the case that you'll need conditions. The issue, I think, is you need to ensure you're able to handle the conditions in the most effective way.

我不会说这是“坏”,因为你需要条件。我认为,问题在于你需要确保自己能够以最有效的方式处理这些情况。

Using case / switch would be a legitimate way of doing this, however, you may have an issue further up the stack

使用case / switch可能是一种合法的方法,但是,您可能会在堆栈的后面有问题

#1


2  

Statement

声明

Maybe you're looking for case / switch:

也许你在寻找箱子/开关:

case current_user.role_id
when 4
  #do something for 4 
when 1
  #do something for 1
end

--

- - -

System

系统

I would highly recommend using a partial to get this to work:

我强烈建议使用一个部分来让它工作:

<%= render partial: "your_partial", local: { your_local_var: "value" } %>

This will allow you to define the conditions in the partial itself:

这将使您能够定义部分本身的条件:

#app/views/controller/your_partial.html.erb
<% if var == "value" %>
  ...
<% else %>
  ...
<% end %>

Bad?

坏的?

I wouldn't say it's "bad" for the sake of it - it's typically the case that you'll need conditions. The issue, I think, is you need to ensure you're able to handle the conditions in the most effective way.

我不会说这是“坏”,因为你需要条件。我认为,问题在于你需要确保自己能够以最有效的方式处理这些情况。

Using case / switch would be a legitimate way of doing this, however, you may have an issue further up the stack

使用case / switch可能是一种合法的方法,但是,您可能会在堆栈的后面有问题