Rails:非正统网址的资源?

发布于 2024-09-25 03:54:47 字数 123 浏览 8 评论 0原文

我想要一个像这样的 URL:

/payroll/region/1

并且我希望它映射到任务控制器的 payroll_list 函数。我还想使用 REST。最好的方法是什么?

非常感谢!

I'd like to have a URL like this:

/payroll/region/1

and I'd like it to map to the Tasks Controller's payroll_list function. I'd also like to use REST. What's the best way to do this?

Many thanks!

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

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

发布评论

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

评论(2

北音执念 2024-10-02 03:54:47

好吧,我建议你最好遵循 Rails 处理这个问题的惯例。如果您仍然坚持使用此类“奇怪”的 URL,并希望忽略这在进一步开发过程中可能产生的问题/头痛,那么请尝试使用 Refraction。

我不想无礼,但目前在我看来,您不明白为什么静态 URL 是这样的。请首先了解其背后的设计,然后重新考虑您的应用程序/控制器和路由设计。我打赌你会开悟的。

在此示例中,您的 URL 可能应为 /regions/1/payrolls 以及 map.resources :regions, :has_many => :工资单。然后,您的工资单列表将由具有 params[:region_id] 的 PayrollsController 呈现 - 这实际上是有意义的(可能也是您尝试通过 URL 布局实现的目标) 。代码片段:

def index
  if params[:region_id]
    @region = Region.find(params[:region_id])
    @payrolls = @region.payrolls
  else
    @payrolls = Payroll.all
  end
end

如果您仍然希望在不同的命名 URL 下拥有资源,请使用以下命令:

map.resources :regions do |regions|
  regions.resources :tasks, :as => :payrolls
end

这将使用命名 URL 部分“工资单”将嵌套资源映射到任务控制器。但这可能不会像您期望的那样工作,因为安静的逻辑意味着您应该在 PayrollsController 中处理工资模型。否则你可能会遇到奇怪的代码。也许您的 TasksController 设计是错误的?尽管您将其命名为工资单,Rails 可能会期望任务被处理到您的任务控制器。这至少可能会令人困惑(但是,它实际上并不期望这些是任务模型,因此它可能会起作用)。

顺便说一句 - 请记住:“restful”还意味着您的应用程序应该响应资源上的标准动词,而不仅仅是使用“resourceful”路由。它还涉及 GET、PUT、DELETE 和 POST http 动词,当然还有“编辑”、“新建”等默认操作。不要试图让你的控制器变得又大又复杂。遵循“瘦控制器 - 胖模型”的座右铭。

Well I'd suggest you better go with the convention how Rails handles this. If you still insist on using such "strange" URLs and want to ignore the problems/headaches this can create during further development, then try to use Refraction.

I don't want to be rude but currently it seems to me that you did not understand why restful URLs are the way they are. Please do understand the design behind this first, then rethink your application/controller and routing design. I bet you will be enlighted.

In this example, your URL should probably be /regions/1/payrolls with map.resources :regions, :has_many => :payrolls. Then your payroll list would be rendered by the PayrollsController having a params[:region_id] - and that actually makes sense (and probably what you tried to achieve with your URL layout). Code snippet:

def index
  if params[:region_id]
    @region = Region.find(params[:region_id])
    @payrolls = @region.payrolls
  else
    @payrolls = Payroll.all
  end
end

If you still want to have a resource under a different named URL, use the following:

map.resources :regions do |regions|
  regions.resources :tasks, :as => :payrolls
end

This will map the nested resources to the tasks controller using the named URL part "payrolls." But this probably does not work as you might expect because restful logic means you should handle the payroll model in the PayrollsController. Otherwise you might run into strange looking code. Maybe your design of the TasksController is just wrong? Rails will probably expect tasks to be handled over to your tasks controller although you name it payrolls. This can be confusing at least (however, it does not actually expect these being task models, so it will probably work).

BTW - Keep in mind: "restful" also means your application should answer to standard verbs on a resource, not just using "resourceful" routes. It's also about the GET, PUT, DELETE and POST http verbs, and of course the "edit", "new" etc default actions. Do not try to make your controllers big and complicated. Follow the motto "skinny controllers - fat models".

烂人 2024-10-02 03:54:47

好的,一个更好的问题可能是这样的:

我怎样才能得到它,以便我使用您的建议:

/regions/1/payroll

并将其 RESTful 映射到:

带有索引、新等的任务控制器,前缀为“工资单_”?

像这样:TasksController#payroll_index 或 TasksController#payroll_new

OK, so a better question, then might be this:

How can I get it so that I use your suggestion:

/regions/1/payroll

and have that map RESTfully to:

Tasks controller with index, new, etc that are prefixed by "payroll_"?

Like this: TasksController#payroll_index or TasksController#payroll_new

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