简体   繁体   中英

Rails: Limited access to pages for not logged users

I would like to restrict an access for users without accounts to some pages (under one controller). Eg: user who is not logged in can check those pages 10 times per day. If this number will be exceeded he will be automatically redirect to page informing him about daily limit and encouraging him to register an account.

Are there any gems to accomplish it? (I use devise)

My initial notion was to build an additional db-table storing IP addresses (using request.remote_ip method) for viewers who are not signed in. Every time unlogged viewer visit the website rails app will launch a method checking if his IP appeared there 10 times in last day or not. Is this a good soluction?

Rails ver: 5.2.1 Devise ver: 4.2

The problem with using IP address is that you can have many users on the same IP address if they're on a shared IP so that is not a good solution for production. Instead you should probably use https://github.com/cbeer/devise-guests gem.

The approach here is that you can still have a user session for non-logged in users.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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