每个用户的 Rails 身份验证礼物清单?

发布于 2024-09-28 07:08:34 字数 1109 浏览 4 评论 0原文

我正在尝试掌握 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 上发布。

http://giftapp.heroku.com/

http://github.com/dannyweb/GiftApp

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.

http://giftapp.heroku.com/

http://github.com/dannyweb/GiftApp

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

段念尘 2024-10-05 07:08:34

这行得通吗,还是我失踪了
东西?

非常简短的回答:是的,那会起作用;不,你没有错过什么。


我看了你的代码。

而不是:

def index
  @people = Person.find(:all)
end

您需要类似以下内容:

def index
  @people = current_user.people
end

其中 current_user 是引用登录用户的 User 对象。

create 方法中,您需要将新创建的人员分配给 current_user:

def create
  @person = Person.new(params[:person])
  @person.user = current_user # This associates @person with current_user
  if @person.save
    flash[:notice] = "Successfully created person."
    redirect_to @person
  else
    render :action => 'new'
  end
end

Would that work, or am I missing
something?

Very short answer: yes that would work; no you are not missing something.


I looked at your code.

Instead of:

def index
  @people = Person.find(:all)
end

You need something along the lines of:

def index
  @people = current_user.people
end

Where current_user is the User 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:

def create
  @person = Person.new(params[:person])
  @person.user = current_user # This associates @person with current_user
  if @person.save
    flash[:notice] = "Successfully created person."
    redirect_to @person
  else
    render :action => 'new'
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文