Ruby on rails 如果我可以添加“;如果”;在“之后;行动前”;红矿控制器

Ruby on rails 如果我可以添加“;如果”;在“之后;行动前”;红矿控制器,ruby-on-rails,Ruby On Rails,我想给我的redmine插件添加权限,我设置了一个方法,在三种不同的情况下返回三个不同的值。不同的值意味着不同的权限,但是我没有得到关于代码的语法错误,我不知道 我不知道如何修复它 class TextpollsController < ApplicationController before action: if :the_value_of_the_role_id==6 :only => [:index,:vote,:setting]

我想给我的redmine插件添加权限,我设置了一个方法,在三种不同的情况下返回三个不同的值。不同的值意味着不同的权限,但是我没有得到关于代码的语法错误,我不知道 我不知道如何修复它

class TextpollsController < ApplicationController
  before action:  if :the_value_of_the_role_id==6
                    :only => [:index,:vote,:setting]
                  elsif :the_value_of_the_role_id==7
                    :only => [:index]
                  elsif
                    deny_access
                  end

  def index
    @textpolls = Textpoll.all
  end
  ...

#the code of the_value_of_the_role_id
def the_value_of_the_role_id
    @user = User.current
    role_id=nil
     Textmember.each do|member|
       if member.user_id==@user.id
         role_id=member.role_id
       end
     end
    return role_id
  end
class TextpollsController[:索引,:投票,:设置]
elsif:角色id的值=7
:only=>[:index]
埃尔西夫
拒绝访问
结束
def索引
@textpolls=Textpoll.all
结束
...
#角色id的值代码
定义角色id的值
@user=user.current
角色id=nil
Textmember.each do| member|
如果member.user\u id==@user.id
role\u id=member.role\u id
结束
结束
返回角色\u id
结束

这是回调源代码:

您可以看到
only
except
只是
if
except
语句。 所以,你可以这样做:

class TextpollsController < ApplicationController
  before_action :your_method, if: :should_be_called?

  def index
    @textpolls = Textpoll.all
  end

private

  def your_method
    # You can add your code here, it will be executed only if the role_id == 6
    # and if the action is :index, :vote or :setting
    # or if the role_id is == 7 and the action is :index
  end

  def should_be_called?
    # action_name is an attribute defined in rails to get the controller’s action name
    if (params[:role_id] == 6 && %w[index vote setting].include?(action_name)) ||
       (params[:role_id] == 7 && action_name == 'index')
      true
    else
      false
    end
  end
end
class TextpollsController
根据编译器,错误发生在“:only”上,不要让自己发疯。使用授权库:谢谢你的帮助,我按照你说的修复了我的代码,语法错误已经消失,但我仍然无法实现权限管理。所有用户都可以执行“索引投票设置”操作,我的方法“角色id的值”中可能有一些错误.在这个插件中,我设置了一个名为textmember的模型,它包含两个变量user_id和role_id。user_id等于user.current.id,如果textmember的role_id等于6,他们可以进行“索引投票设置”,如果role_id等于7,他们可以进行“索引”,否则没有权限。请给我一些建议,谢谢。好的,所以这不是处理它的好方法。我的意思是,你可以用这种方法来实现它,但是如果你必须为每个控制器做这样的事情,即使有顾虑,维护起来也不是那么清晰和无聊。请考虑使用CcChanCAN宝石。有了这个gem,您将拥有一个能力模型,在其中定义所有能力(tadam),然后将其加载到控制器中。如果不允许用户访问某个操作,则可以引发异常,然后将用户重定向到所需的位置。