简体   繁体   English

你如何在Ruby on Rails中编写一个内联的条件三元运算符?

[英]How do you write a conditional ternary operator inline in Ruby on Rails?

I am trying to compare 2 values and change the color of one when the condition holds true. 我试图比较2个值,并在条件成立时更改一个颜色。 But I also have a search-bar and I could not access the tag values when it is wrapped in an <% if %> statement in my javascript function using Rails. 但我也有一个搜索栏,当我使用Rails将其包含在我的javascript函数中的<%if%>语句中时,我无法访问标记值。

I originally had the code as so 我最初的代码是这样的

 <% if track.user.username == current_user.username%>
   <td class="track_table_current_user">
     <span><%= track.user.username %></span>
   </td>
 <% else %>
   <td class="td-userName"> 
     <span class="track_table_user_name"><%= track.user.username %> </span>
   </td>
 <% end %> 

My Ternary as of now: 我现在的三元:

<td class="track_table_user_name <% track.user.username == current_user.username ? "this_current_user" : "other_user" %>">
    <%= track.user.username %>
</td>

The Search Function that I cannot access the values if I use an <% if %> <% else%>: 如果我使用<%if%> <%else%>,则无法访问值的搜索功能:

function trackTableSearch() {
  var input, filter, table, tr, td, i, txtValue;
  input = document.getElementById("trackTableInput");
  filter = input.value.toUpperCase();
  table = document.getElementById("tracksTable");
  tr = table.getElementsByTagName("tr");
  for (i = 0; i < tr.length; i++) {
    tdN  = tr[i].getElementsByClassName("td-trackName")[0];
    tdU = tr[i].getElementsByClassName("track_table_user_name")[0];
    tdR = tr[i].getElementsByClassName("td-trackApproved")[0];
    tdP = tr[i].getElementsByClassName("td-public")[0];
    if ((tdN) || (tdU) || (tdR) || (tdP)) {
      txtValueN  = tdN.textContent  || tdN.innerText;
      txtValueU  = tdU.textContent  || tdU.innerText;
      txtValueA  = tdR.textContent  || tdR.innerText;
      txtValueP  = tdP.textContent  || tdP.innerText;

      if (txtValueN.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      }else if (txtValueU.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else if (txtValueA.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      }else if (txtValueP.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      }else{
        tr[i].style.display = "none";
      }
    }
  }

You probably want to do something like: 你可能想做类似的事情:

<td class="<%= track.user.username == current_user.username ? "track_table_current_user" : "td-userName" %>">
    <%= track.user.username %>
</td>

See this answer to learn how to use the ternary operator, it works about the same in every language out there. 请参阅此答案以了解如何使用三元运算符,它在每种语言中的工作方式大致相同。

I would not use the ternary operator here because it makes your template code much more difficult to read. 我不会在这里使用三元运算符,因为它使您的模板代码更难以阅读。 Instead, I would do an if-else statement. 相反,我会做一个if-else语句。 The ternary operator is really nice for very short if-else statements, but especially in rendered code, we want to reduce all logic possible, and when it's not possible, we want it to be as easy to parse as possible. 三元运算符非常适用于非常短的if-else语句,但特别是在渲染代码中,我们希望减少所有可能的逻辑,并且当它不可能时,我们希望它尽可能容易解析。

This is much easier to parse: 这更容易解析:

<% if track.user.username == current_user.username %>
  <td class="track_table_user_name <%= this_current_user %>>
<% else %>
  <td class="track_table_user_name <%= other_user %>>
...

I would even go as far as to make a @rendered_username variable in the controller so you don't have to do any of this logic. 我甚至会在控制器中创建@rendered_username变量,因此您不必执行任何此逻辑。

I figured it out.... I am going to hate to admit this but i forgot the '=' symbol... uggggg why!?!?! 我想通了......我不想承认这一点,但我忘记了'='符号......为什么!?!?!

<td class="track_table_user_name \
    <% ***(right here)*** track.user.username == current_user.username ? "this_current_user" : "other_user" %>">
     <%= track.user.username %>
    </td>

Thanks everyone for your help and the .inspect, and to put a variable in the controler. 感谢大家的帮助和.inspect,并在控制器中添加一个变量。

My working final : 我的工作决赛:

<td class="track_table_user_name <%= (track.user.username == current_user.username) ? "this_current_user" : "other_user" %>"><%= track.user.username %></td>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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