如何在 Ruby on Rails 3 中为每个用户连续编号项目?

发布于 2025-01-07 01:33:07 字数 437 浏览 0 评论 0原文

我对 Ruby on Rails 3 相当陌生,刚刚开始构建一个小应用程序,用户可以在其中管理他们的项目和任务。

显然,所有项目和任务都必须对公众隐藏,并且只能对创建它们的用户可见。

但我发现这很难实现。例如,如果一个用户创建了他的第一个项目,则可以通过 URL /projects/1 访问该项目。如果五秒后另一个用户创建了她的第一个项目,它将出现在 /projects/2 下。

如何为每个用户对项目进行连续编号,始终从 1 或更有意义的数字开始?

另外,我应该如何布局我的资源?

我在想这样的事情:

resources :users do
  resources :projects
  resources :tasks
end

这是一个好的开始方式吗?

I am fairly new to Ruby on Rails 3 and just started to build a little application where Users can manage their Projects and Tasks.

Obviously all Projects and Tasks will have to be hidden from public and should only be visible to the User who created them.

I find this quite difficult to realise though. For example if one User creates his first Project it will be accessible via the URL /projects/1. If another User creates her first Project five seconds later it will appear under /projects/2.

How can I number Projects consecutively for each User, always starting at 1 or something more meaningful?

Also, how should I lay out my resources?

I was thinking of something like this:

resources :users do
  resources :projects
  resources :tasks
end

Is this a good way to start?

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

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

发布评论

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

评论(4

祁梦 2025-01-14 01:33:07

您可以有两个 id,真正隐藏的 id 和公开可见的 consecutive_id。然后,在您的操作中,您必须执行与此类似的操作:

def show
  id = params[:id]

  # instead of
  #   project = Project.find(id)
  # you now do this
  project = Project.where(user_id: current_user.id, consecutive_id: id).first

  # proceed
end

如果您遵循此路径,则必须自己强制执行 id 的顺序。例如,使用 before_create 回调。

You can have two ids, real hidden id and publicly visible consecutive_id. Then, in your actions, you'll have to do something similar to this:

def show
  id = params[:id]

  # instead of
  #   project = Project.find(id)
  # you now do this
  project = Project.where(user_id: current_user.id, consecutive_id: id).first

  # proceed
end

If you follow this path, you'll have to enforce sequentiality of ids yourself. Use before_create callback, for example.

爱格式化 2025-01-14 01:33:07

首先,我将在 before_create 过滤器中获取最高值,然后生成一个新数字。

class Project < ActiveRecord::Base
  belongs_to :user

  before_create :set_per_user_id

  private
    def set_per_user_id
      val = user.projects.maximum(:document_id)
      self.document_id = val + 1
    end
end

我没有测试代码,但它应该大致是这样工作的。
对 document_id 字段设置一些验证以确保每个用户的唯一性也是一个好主意。

(到目前为止你的路线没问题)

For a start I would get the highest value in a before_create filter and then generate a new number.

class Project < ActiveRecord::Base
  belongs_to :user

  before_create :set_per_user_id

  private
    def set_per_user_id
      val = user.projects.maximum(:document_id)
      self.document_id = val + 1
    end
end

I didn't test the code but it should be roughly working like that.
Setting some validation on the document_id field to ensure uniqueness per user would be a good idea too.

(Your routes are ok so far)

一身仙ぐ女味 2025-01-14 01:33:07

thorsten 的代码将用于设置项目的 document_id 但您仍然需要重新定义在编辑、更新、显示和销毁控制器操作中加载资源的方式,例如:

def show
  @user = User.find params[:user_id]
  @project = @user.projects.find_by_document_id params[:id]
  render
end

老实说,除非这些 id 是连续的用户是一个巨大的需求,我不建议这样做。它打破了 Rails 约定,如果您尝试使用 cancan 之类的库来授权访问,将会变得很痛苦到资源。

thorsten's code will work for setting the project's document_id but you will still have to redefine how the resource is loaded in the edit, update, show and destroy controller actions, e.g.:

def show
  @user = User.find params[:user_id]
  @project = @user.projects.find_by_document_id params[:id]
  render
end

Honestly, unless having these id's consecutive for the user is a huge requirement, I would not suggest doing it this way. It breaks the rails convention and will make it a pain if you are trying to use libraries like cancan to authorize access to resources.

终弃我 2025-01-14 01:33:07

数据库驱动的应用程序设计的一个重要部分是,您应该将模型(项目、任务等)的数据库 ID 视为随机数。 它们没有任何意义。

从用户界面的角度来看,您还应该将应用程序使用的 URL 视为随机字符串。也就是说,您的最终用户永远不应该知道或关心给定信息屏幕的 URL 是什么。

如果您接受上述良好的设计实践,那么:

创建新项目时,您应该使用过滤器或“工厂”方法来确定项目的“project_number”字段的值。例如,对于给定用户,它们应该是连续的。但您还必须决定如果用户删除项目应该做什么。您希望重复使用该项目的project_number 吗?

您在问题中似乎暗示的另一个问题是权限:用户 1 是否应该能够查看用户 2 的项目/任务/等?如果没有,则使用 cancan 等 gem 或 类似 来控制权限。

第三个可能的问题是您的项目将使用的 URL。标准 UX 原则是忽略 url 格式,正如我上面提到的。但如果你真的愿意,你可以让它们变得“漂亮”。有许多宝石可以帮助解决这个问题。请参阅列表

An important part of database-driven application design is that you should think of the database ids of your Models (Projects, Tasks, etc) as being random numbers. They mean nothin'.

And from a User Interface point of view, you should also think of the urls that your application uses as being random strings. That is, your end users should never know or care what the urls are for a given screen of information.

If you accept the above good design practices, then:

When a new Project is created, you should use filters or a "Factory" method to determine the value for the Project's "project_number" field. Eg, for a given user, they should be consecutive. But you also have to decide what should be done if a project is deleted by a user. Do you want that project's project_number to be re-used?

A different issue that you seem to hint at in the question is permissions: should user 1 be able to see user 2's projects/tasks/etc? If not, then use a gem such as cancan or similar to control permissions.

A third possible issue is the urls that will be used by your project. Standard UX principle is to ignore url formats, as I note above. But if you really want to, you can make them "pretty." There are a number of gems to help with this. See list

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文