我的事件的 Rails 控制器设计模式
我想要一个 My_Events 页面,除了显示系统中所有事件的索引页面之外,它只显示当前用户订阅的事件,并允许用户操作这些事件。
我应该如何去做才能符合 RESTful 模式?我应该只创建一个 my_events 操作和 my_event.html.erb,还是为其创建一个新控制器?选择一种方法而不是另一种方法的原因是什么?
谢谢。
I would like to have a My_Events page, which only display events that current user subscribed to, and allow user to manipulate these events, in addition to Index page that displays all events in the system.
How should I go about this to comply with RESTful pattern? Should I just create a my_events action and my_event.html.erb, or create a new controller for it? What would be the reason to choose one over another approach?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您碰巧有一个用户控制器,那么向其中添加事件操作并根据用户特定事件的用户进行路由似乎是最 RESTful 的。
与 /events/my_events 之类的东西相比,这样做的好处是 URL 对于所呈现的数据来说是唯一的。如果用户使用链接 /users/fubar/events,那么它将显示用户 fubar 的事件,而与 /events/my_events 之类的内容一样,生成的页面与会话相关联,而不是与 URL 相关联。
当然,这实际上取决于您的目标是什么以及您的应用程序有什么要求。
If you happen to have a user controller, adding an events action to that and routing based on the user for user specific events would seem most RESTful.
The benefit to this over something like /events/my_events is that the URL is unique to the data that is being presented. If a user uses the link /users/fubar/events then it will show events for the user fubar, where as with something like /events/my_events the generated page is tied to the session and not the URL.
Of course it really depends on what your goal is, and what requirements your application has.
您可以在另一个控制器中使用方法来加载这些事件并执行您想要/需要的所有操作。但是,如果您想使用索引、编辑、删除和创建新事件,我建议您使用 events_controller,因为将来添加一行又一行的代码时,它会对您更好。
这就是每个模型都有一个控制器、让所有东西都有序的要点。
You can have methods in another controller that load these Events and do all the things that you want/need. But if you want to use the index, edit, delete and create new Event, I would recommend having an events_controller as it will be better for you in the future when lines and lines of codes are added.
That's the point of having a controller per model, having everything ordered.