Search code examples
ruby-on-railsternary

rails syntax for block in ternary


I have nested loop blocks in a view with a ternary in the secondary block. This throws me a syntax error, unexpected keyword_do_block, expecting ':'.

<% @teams.each do |team| %>
  some html
  <% @account.some_attribute true?  ? team.users.with_option.sort_lastname.each do |user| : team.users.active.sort_lastname.each do |user] %>
    some html
  <% end %>
<% end %>

Is there a correct syntax for this or should I find another way ?


Solution

  • No, you should do something like following

    You should take array of users in some_variable for ex:- users and then iterate loop over it

    something like this

    <% @teams.each do |team| %>
      some html
      <% users =  @account.some_attribute true?  ? team.users.with_option.sort_lastname : team.users.active.sort_lastname
       users.each do |user| %>
        some html
      <% end %>
    <% end %>