Ruby on rails Ruby:多行中的条件

Ruby on rails Ruby:多行中的条件,ruby-on-rails,ruby,Ruby On Rails,Ruby,在其他编程语言中,我可以通过在逻辑运算符中将条件分解成行,但在ruby(我使用的是rails)中似乎不是这样。是关于代码还是关于语法 <% if (current_user && current_user.id == @user.id) || @user.profile_privacy === 0 || (@user.profile_privacy === 1 && current_user && c

在其他编程语言中,我可以通过在逻辑运算符中将条件分解成行,但在ruby(我使用的是rails)中似乎不是这样。是关于代码还是关于语法

<% if (current_user && current_user.id == @user.id)
   || @user.profile_privacy === 0 
   || (@user.profile_privacy === 1 
      && current_user 
      && current_user.user_friendships.find_by_friend_id(@user.id)
      && current_user.user_friendships.find_by_friend_id(@user.id).state == true ) %>

视图中不应包含所有逻辑,但要修复代码,请将运算符移到行的末尾:

<% if (current_user && current_user.id == @user.id) || 
  @user.profile_privacy === 0 || 
  (@user.profile_privacy === 1 && 
  current_user && 
  current_user.user_friendships.find_by_friend_id(@user.id) &&  
  current_user.user_friendships.find_by_friend_id(@user.id).state == true ) 
%>

视图中不应包含所有逻辑,但要修复代码,请将运算符移到行的末尾:

<% if (current_user && current_user.id == @user.id) || 
  @user.profile_privacy === 0 || 
  (@user.profile_privacy === 1 && 
  current_user && 
  current_user.user_friendships.find_by_friend_id(@user.id) &&  
  current_user.user_friendships.find_by_friend_id(@user.id).state == true ) 
%>

您应该尽量避免模板中的逻辑

例如,您可以将一些逻辑移到
用户
模型中:

class User < ActiveRecord::Base
  def has_user_as_friend user
    user_friendships.find_by_friend_id(user).state == true
  end

  def is_private
    profile_privacy === 0
  end
end
然后,您的情况可能会变得简单一些:

<% if (@is_current_user || @user.is_private) %>


我没有完全复制您的条件,但希望这能为您提供一些想法,让您了解如何简化
if
语句。

您应该尽量避免模板中的逻辑

例如,您可以将一些逻辑移到
用户
模型中:

class User < ActiveRecord::Base
  def has_user_as_friend user
    user_friendships.find_by_friend_id(user).state == true
  end

  def is_private
    profile_privacy === 0
  end
end
然后,您的情况可能会变得简单一些:

<% if (@is_current_user || @user.is_private) %>


我没有完全复制您的条件,但希望这能给您一些想法,让您了解如何简化
if
语句。

您应该尝试将此逻辑从模板中移到控制器中。我不知道如何,我认为这会非常复杂,因为它是关于在不同情况下返回的不同html的。不管怎样,你知道我该怎么解决这个问题吗@SamI正在给你写一个例子,给我几分钟:)你应该试着把这个逻辑从模板中移到你的控制器中。我不知道怎么做,我认为这会很复杂,因为它是关于在不同情况下返回的不同html。不管怎样,你知道我该怎么解决这个问题吗@萨米只是给你写了一个例子,给我几分钟:)对不起-我知道这不是一个确切的答案,就在上面。但是,我认为这是值得一提的!抱歉-我知道这不是一个确切的答案,infused就在上面。但是,我认为这是值得一提的!