我是ruby的新用户,并拥有一个应用程序,其中用户是" plan_id = 1"或" plan_id = 2"。
我有一个部分使用:<% @users.each do |user| %>
来显示所有用户。
我想让这个页面只能向&#34; plan_id 2&#34;中的用户查看,同时只显示来自&#34; Plan_id 1&#34;的用户。
简单我确定,但我是新的,所以任何建议都表示赞赏。
我该怎么做?
答案 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 %>