I want to link a model Tasks to a User properly, to make sure that only an authenticated user can access only the Tasks and dependent models that belong_to User and Task
I think you are looking for an authorization solution. If this is a simple app, you should set up your controllers to return only tasks belong to the user like so:
@tasks = current_user.tasks
Presumably you have a has_many association set up on your User model. If you do this for all your controllers, they will be scoped to your user, so you don't have to worry about authorization.
If you are looking for a more complex solution, look towards something like cancan.
发布评论
评论(1)
我认为您正在寻找授权解决方案。如果这是一个简单的应用程序,您应该将控制器设置为仅返回属于用户的任务,如下所示:
假设您在 User 模型上设置了 has_many 关联。如果您对所有控制器执行此操作,它们的范围将仅限于您的用户,因此您不必担心授权问题。
如果您正在寻找更复杂的解决方案,请寻找类似 cancan 的解决方案。
I think you are looking for an authorization solution. If this is a simple app, you should set up your controllers to return only tasks belong to the user like so:
Presumably you have a has_many association set up on your User model. If you do this for all your controllers, they will be scoped to your user, so you don't have to worry about authorization.
If you are looking for a more complex solution, look towards something like cancan.