简体   繁体   English

Rails - 如何使用请求引用来限制基于引用者的视图访问

[英]Rails - how to use request referrer to limit access to a view based on referrer

I've been looking around for this but can't seem to find a solution for what I'm trying to do. 我一直在寻找这个,但似乎无法找到我想要做的解决方案。

I have a 'thankyou' view routed from the custom controller action. 我有一个从自定义控制器操作路由的'thankyou'视图。 I want to restrict access to this view to only the case where the user came from the view where the user submitted the form since I don't want users to be able to navigate directly to this view. 我想限制对此视图的访问仅限于用户来自用户提交表单的视图的情况,因为我不希望用户能够直接导航到此视图。

How would I write the controller code to accomplish this in the custom controller action? 如何编写控制器代码以在自定义控制器操作中完成此操作?

In your controller you can do: 在您的控制器中,您可以:

def thankyou
  if request.referrer != "http://mysite.com/myformpage"
    redirect_to root_path, notice: "Invalid access"
  end
end

Or you can create a before_filter for this action with the same logic 或者,您可以使用相同的逻辑为此操作创建before_filter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM