I'm trying to build a condition based on wether or not a "user" is a "member". Basically I need a way of checking if the current_user.id matches any of the user_id of any members. The non-working code I have right now is:
我正在尝试建立一个基于或不是“用户”是“成员”的条件。基本上我需要一种方法来检查current_user.id是否匹配任何成员的任何user_id。我现在的非工作代码是:
<% if current_user = @page.members %>
you can view this content.
<% end %>
I'm looking for something along the lines of: "If current_user.id exists in the "user_id" of any members."
我正在寻找以下内容:“如果current_user.id存在于任何成员的”user_id“中。”
4 个解决方案
#1
13
Something like this, based on the field names in your question:
这样的事情,基于你问题中的字段名称:
<% if @page.members.map(&:user_id).include? current_user.id %>
You can view this content
<% end %>
#2
4
Assuming your @page.members
variable contains an array, you can use the include?
method:
假设你的@ page.members变量包含一个数组,你可以使用include吗?方法:
<% if @page.members.include? current_user %>
you can view this content.
<% end %>
If you're using an array of ids, you will of course need to change the test slightly to look for the current user's id:
如果您正在使用一组id,您当然需要稍微更改测试以查找当前用户的ID:
<% if @page.members.include? current_user.id %>
you can view this content.
<% end %>
#3
1
@member_ids = @page.members.map{|m| m.id()}
then check for the condition as below
然后检查以下条件
@memeber_ids.include? current_user.id()
#4
0
Has said before include?
should do the thing.
之前有说过吗?应该做的事情。
I'm just answering to tell you about a gem called CanCan, that gives you easy access for authorization "helpers".
我只是回答告诉你一个名为CanCan的宝石,让你轻松访问授权“助手”。
Why you should use CanCan instead of doing what you are actually doing?
为什么你应该使用CanCan而不是做你实际做的事情?
- Don't reinventing the weel most of the times it's a goob practice.
- 不要在大多数时候重新发明它是一个goob练习。
- You are placing business logic on the view (bad practice).
- 您将业务逻辑放在视图上(不良做法)。
- CanCan most likely has been developed thinking on security, and all the best practices in mind.
- CanCan很可能已经开发出关于安全性和所有最佳实践的思考。
- You save some developing hours.
- 你节省了一些开发时间。
Sorry if I repeated myself.
对不起,如果我重复自己。
#1
13
Something like this, based on the field names in your question:
这样的事情,基于你问题中的字段名称:
<% if @page.members.map(&:user_id).include? current_user.id %>
You can view this content
<% end %>
#2
4
Assuming your @page.members
variable contains an array, you can use the include?
method:
假设你的@ page.members变量包含一个数组,你可以使用include吗?方法:
<% if @page.members.include? current_user %>
you can view this content.
<% end %>
If you're using an array of ids, you will of course need to change the test slightly to look for the current user's id:
如果您正在使用一组id,您当然需要稍微更改测试以查找当前用户的ID:
<% if @page.members.include? current_user.id %>
you can view this content.
<% end %>
#3
1
@member_ids = @page.members.map{|m| m.id()}
then check for the condition as below
然后检查以下条件
@memeber_ids.include? current_user.id()
#4
0
Has said before include?
should do the thing.
之前有说过吗?应该做的事情。
I'm just answering to tell you about a gem called CanCan, that gives you easy access for authorization "helpers".
我只是回答告诉你一个名为CanCan的宝石,让你轻松访问授权“助手”。
Why you should use CanCan instead of doing what you are actually doing?
为什么你应该使用CanCan而不是做你实际做的事情?
- Don't reinventing the weel most of the times it's a goob practice.
- 不要在大多数时候重新发明它是一个goob练习。
- You are placing business logic on the view (bad practice).
- 您将业务逻辑放在视图上(不良做法)。
- CanCan most likely has been developed thinking on security, and all the best practices in mind.
- CanCan很可能已经开发出关于安全性和所有最佳实践的思考。
- You save some developing hours.
- 你节省了一些开发时间。
Sorry if I repeated myself.
对不起,如果我重复自己。