使用users.each显示特定的用户计划

时间:2017-01-30 15:00:35

标签: ruby-on-rails ruby devise

我是ruby的新用户,并拥有一个应用程序,其中用户是" plan_id = 1"或" plan_id = 2"。

我有一个部分使用:<% @users.each do |user| %>来显示所有用户。

我想让这个页面只能向&#34; plan_id 2&#34;中的用户查看,同时只显示来自&#34; Plan_id 1&#34;的用户。

简单我确定,但我是新的,所以任何建议都表示赞赏。

我该怎么做?

1 个答案:

答案 0 :(得分:3)

您可以创建范围以获取plan_id = 1

的用户

<强>控制器

@users = User.where(plan_id: 1)

# or create a scope

scope premium_users, -> { where(plan_id: 1) }

def your_method
  @users = User.premium_users
  # ...
end

检查当前用户是否有plan_id = 2,然后输入块

查看

<% if current_user.plan_id == 2 %>
  <% @users.each do |user| %>
    # Your stuff here
  <% end %>
<% end %>