Ruby on rails 验证控制器中的用户,然后重定向
我在Ruby on rails 验证控制器中的用户,然后重定向,ruby-on-rails,ruby,model-view-controller,controller,Ruby On Rails,Ruby,Model View Controller,Controller,我在projects\u controller.rb下有以下代码 class Admin::ProjectsController < ApplicationController before_filter :require_login def require_login while (adminLogin != 'username') redirect_to admin_login_path and return end end class ApplicationCon
projects\u controller.rb下有以下代码
class Admin::ProjectsController < ApplicationController
before_filter :require_login
def require_login
while (adminLogin != 'username')
redirect_to admin_login_path and return
end
end
class ApplicationController < ActionController::Base
# Return the admin user
def adminLogin
@email = params[:email]
@password = params[:password]
return @email
end
end
class ApplicationController < ActionController::Base
protect_from_forgery
# Return the admin user
def redirect_unless_admin
@email = params[:email]
password = params[:password]
if (@email == 'username')
redirect_to admin_projects_path
else
redirect_to admin_login_path
end
我正在尝试以这种形式获取电子邮件,并将其传递给项目控制器,以便在定义电子邮件时,管理员可以登录。当我按下表单上的submit按钮时,我可以看到使用表单中的
将正确的电子邮件发送到项目控制器,但页面会重定向以再次登录。我怎样才能进入项目
[更新]:
application\u controller.rb
class Admin::ProjectsController < ApplicationController
before_filter :require_login
def require_login
while (adminLogin != 'username')
redirect_to admin_login_path and return
end
end
class ApplicationController < ActionController::Base
# Return the admin user
def adminLogin
@email = params[:email]
@password = params[:password]
return @email
end
end
class ApplicationController < ActionController::Base
protect_from_forgery
# Return the admin user
def redirect_unless_admin
@email = params[:email]
password = params[:password]
if (@email == 'username')
redirect_to admin_projects_path
else
redirect_to admin_login_path
end
class ApplicationController
结束
结束
我需要在我的项目\u controller.rb
中使用此方法。这只是破坏了它,重定向了太多次如果需要从ProjectsController调用adminLogin,则应该在ApplicationController或从ApplicationController派生的公共父类中定义它。如果需要从ProjectsController调用adminLogin,它应该在ApplicationController中定义,或者在从ApplicationController派生的公共父类中定义。您应该能够在application\u controller中使用它:
class ApplicationController < ActionController::Base
def redirect_unless_admin
email = params[:email]
password = params[:password]
if params[:email].present? && email == 'username'
redirect_to admin_projects_path
else
redirect_to root_path
end
end
end
class Admin::ProjectsController < ApplicationController
before_filter :redirect_unless_admin
end
class ApplicationController
在管理员/项目\控制器中:
class ApplicationController < ActionController::Base
def redirect_unless_admin
email = params[:email]
password = params[:password]
if params[:email].present? && email == 'username'
redirect_to admin_projects_path
else
redirect_to root_path
end
end
end
class Admin::ProjectsController < ApplicationController
before_filter :redirect_unless_admin
end
class Admin::ProjectsController
这样,重定向\u,除非继承应用程序\u控制器的任何控制器都可以使用\u admin
方法。您可以通过任何方式自定义逻辑,以确定要使用哪种重定向,但这应该是一个很好的起点。您应该能够在应用程序\u控制器中实现这一点:
class ApplicationController < ActionController::Base
def redirect_unless_admin
email = params[:email]
password = params[:password]
if params[:email].present? && email == 'username'
redirect_to admin_projects_path
else
redirect_to root_path
end
end
end
class Admin::ProjectsController < ApplicationController
before_filter :redirect_unless_admin
end
class ApplicationController
在管理员/项目\控制器中:
class ApplicationController < ActionController::Base
def redirect_unless_admin
email = params[:email]
password = params[:password]
if params[:email].present? && email == 'username'
redirect_to admin_projects_path
else
redirect_to root_path
end
end
end
class Admin::ProjectsController < ApplicationController
before_filter :redirect_unless_admin
end
class Admin::ProjectsController
这样,重定向\u,除非继承应用程序\u控制器的任何控制器都可以使用\u admin
方法。您可以通过任何方式自定义逻辑,以确定要使用哪个重定向,但这应该是一个很好的起点。那么我应该将该方法置于applicationController下吗?那么我应该将该方法置于applicationController下吗?通过使用您的确切代码,我得到了一个网站改道太多次
,因此我不确定这是否正确。如果我把你的方法改成这个@email='username'重定向到(@email='username'?admin\u projects\u path:admin\u login\u path)
它得到了正确的电子邮件并路由到/admin/projects
,但是它又崩溃了太多次,重新路由错误很酷,现在试试看。可能是想把它弄得太花哨了。当所有其他方法都失败时,只需使用一个好的旧if/else:)请查看我上面的注释,其中包含更新的代码^^^^您的if/else语句缺少一个结尾。只是这里的格式不正确。if语句中的变量是正确的,因此只是重定向_,我猜它没有将我重定向到那里。由于我在每次发言前都有一个声明,我是否应该告诉项目控制员,电子邮件
现在有了价值?通过使用您的确切代码,我得到了一个网站改道次数过多
,因此我不确定这是否正确。如果我把你的方法改成这个@email='username'重定向到(@email='username'?admin\u projects\u path:admin\u login\u path)
它得到了正确的电子邮件并路由到/admin/projects
,但是它又崩溃了太多次,重新路由错误很酷,现在试试看。可能是想把它弄得太花哨了。当所有其他方法都失败时,只需使用一个好的旧if/else:)请查看我上面的注释,其中包含更新的代码^^^^您的if/else语句缺少一个结尾。只是这里的格式不正确。if语句中的变量是正确的,因此只是重定向_,我猜它没有将我重定向到那里。由于我在每个语句之前都有一个值,我是否应该告诉项目控制者电子邮件
现在有一个值?