每个用户的 Rails 身份验证礼物清单?
我正在尝试掌握 Rails 中身份验证的基础知识。首先,我使用了 Ryan Bates 的 nifty_authentication 生成器。它帮助我学习用户登录等的基本选项。
我有一个简单的应用程序,数据库中有一个人和礼物表。这个想法是,每个用户创建一个人员列表,然后为每个人分配可能的礼物。
所以从结构的角度来看:
person belongs to user
gift belongs to person
所以我的模型设置如下。
人员模型
class Person < ActiveRecord::Base
has_many :gifts
end
礼物模型
class Gift < ActiveRecord::Base
belongs_to :person
end
用户模型
currently doesn't contain any belongs_to has_many etc.
我如何确保每个用户都有自己的人员列表。因此,一个用户无法看到另一用户的人员或礼物列表。
我可以简单地将以下内容添加到用户模型中吗?
has_many :people
以及以下人员模型?
belongs_to :user
这有用吗,还是我错过了什么?
谢谢,
丹尼
更新: 目前该应用程序已在 Heroku 和 Github 上发布。
I am trying to get to grips with the basics of authentication in Rails. To start with I have used the nifty_authentication generator by Ryan Bates. It's helping me learn the basic options for user logins etc.
I have a simple application the has a person and gift table in the database. The idea is, each user creates a list of people and then assigned possible gifts to each of those people.
So from a structural point of view:
person belongs to user
gift belongs to person
So I have the models set up as follows.
person model
class Person < ActiveRecord::Base
has_many :gifts
end
gift model
class Gift < ActiveRecord::Base
belongs_to :person
end
user model
currently doesn't contain any belongs_to has_many etc.
How do I go about making sure each user has their own list of people. So one user cannot see another users list of people or gifts.
Would I simply add the following to the user model?
has_many :people
and the following to the person model?
belongs_to :user
Would that work, or am I missing something?
Thanks,
Danny
UPDATE:
The app so far is on Heroku and Github.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
非常简短的回答:是的,那会起作用;不,你没有错过什么。
我看了你的代码。
而不是:
您需要类似以下内容:
其中
current_user
是引用登录用户的User
对象。在
create
方法中,您需要将新创建的人员分配给 current_user:Very short answer: yes that would work; no you are not missing something.
I looked at your code.
Instead of:
You need something along the lines of:
Where
current_user
is theUser
object that refers to the logged in user.In the
create
method you will need to assign the newly created person to the current_user: