Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何让正则表达式在此rails应用程序中工作?_Ruby On Rails_Regex_Roles - Fatal编程技术网

Ruby on rails 如何让正则表达式在此rails应用程序中工作?

Ruby on rails 如何让正则表达式在此rails应用程序中工作?,ruby-on-rails,regex,roles,Ruby On Rails,Regex,Roles,我不熟悉rails和正则表达式。我正在尝试制作一个应用程序,用户可以使用以下两种电子邮件地址之一进行注册:user@a.edu或user@b.edu. 我正在制作一个页面,显示不是当前用户类型的所有用户。例如jason@a.edu如果用户已登录,该页面将显示类型b的所有用户。如果lauren@b.edu如果用户已登录,则该页面将显示类型a的所有用户。我正在尝试使用正则表达式来了解基于电子邮件地址登录的用户类型,并在用户单击链接时动态生成页面。我在模型中创建了此方法: def other_sch

我不熟悉rails和正则表达式。我正在尝试制作一个应用程序,用户可以使用以下两种电子邮件地址之一进行注册:user@a.edu或user@b.edu. 我正在制作一个页面,显示不是当前用户类型的所有用户。例如jason@a.edu如果用户已登录,该页面将显示类型b的所有用户。如果lauren@b.edu如果用户已登录,则该页面将显示类型a的所有用户。我正在尝试使用正则表达式来了解基于电子邮件地址登录的用户类型,并在用户单击链接时动态生成页面。我在模型中创建了此方法:

def other_schools
   if /.+@a\.edu/.match(current_user.email)
      User.where(email != /.+@a\.edu/)
   else
      render :text => 'NOT WORKING', :status => :unauthorized
   end
end
这是控制器:

def index
    #authorize! :index, :static_pages 
    @users = current_user.other_schools
end
以下是显示每个用户的视图:

<% @users.each do |user| %>
          <li class="span3">
                <div class="thumbnail" style="background: white;">
                  <%= image_tag "idea.jpeg" %>
                  <h3><%= user.role %></h3>
                  <p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
                  <a class="btn btn-primary">View</a>
                </div>
          </li>
<% end %>

  • 我是奥迪奥先生,我是艾格斯塔·艾格特·奎姆先生。Donec在eget metus为非mi porta孕妇确定了最佳身份。我不知道是谁干的,我不知道是谁干的

    看法
  • 视图只是在@user对象中循环。当我尝试加载页面时,我被告知有一个未定义的局部变量或方法“current_user”。如何修复此问题?

    您的模型没有“意识到”helpers方法。当前用户就是其中之一。因此,您需要将用户对象传递给函数/使用当前用户实例来获取结果:

    # controller
    def index
        #authorize! :index, :static_pages
        @users = User.other_schools(current_user)
    end
    
    # User model
    def self.other_schools(user) # class method
       if user.email.match(/.+@a\.edu/)
          User.where("email NOT LIKE '%@a.edu'")
       else
          User.where('false') # workaround to returns an empty AR::Relation
       end
    end
    

    备选方案(使用当前用户实例):

    您的模型不“知道”helpers方法。当前用户就是其中之一。因此,您需要将用户对象传递给函数/使用当前用户实例来获取结果:

    # controller
    def index
        #authorize! :index, :static_pages
        @users = User.other_schools(current_user)
    end
    
    # User model
    def self.other_schools(user) # class method
       if user.email.match(/.+@a\.edu/)
          User.where("email NOT LIKE '%@a.edu'")
       else
          User.where('false') # workaround to returns an empty AR::Relation
       end
    end
    

    备选方案(使用当前用户实例):


    谢谢我尝试实现替代版本,但现在它说视图中有一个未定义的方法“each”。那对我来说毫无意义。我做了一次编辑,将视图放入上面的问题内容中。我刚刚更新了我的答案@Philip7899。问题是你试图在模型中进行渲染,但应该在ControllerHanks中进行。现在它呈现为“不工作”,但实际上它应该工作。我想这和我的正则表达式有关。你知道我的正则表达式是否正确吗?不,它不正确,对于PG SQL,你需要使用以下语法:
    。其中(“email=~?”,/。+@a\.edu/)
    ——你的DBMS是什么?MySQL?PostGreSQL?MongoDB?我想说的是电子邮件是不平等的。谢谢。我尝试实现替代版本,但现在它说视图中有一个未定义的方法“each”。那对我来说毫无意义。我做了一次编辑,将视图放入上面的问题内容中。我刚刚更新了我的答案@Philip7899。问题是你试图在模型中进行渲染,但应该在ControllerHanks中进行。现在它呈现为“不工作”,但实际上它应该工作。我想这和我的正则表达式有关。你知道我的正则表达式是否正确吗?不,它不正确,对于PG SQL,你需要使用以下语法:
    。其中(“email=~?”,/。+@a\.edu/)
    ——你的DBMS是什么?MySQL?PostGreSQL?MongoDB?我想说的是电子邮件是不平等的。