我应该如何为具有相同操作的不同视图的模型组织控制器?
我有一个用户模型,在我看来,它有两个显示操作:
- 对用户自己的记录执行“显示”(以查看他们的私人个人资料)
- 显示”执行其他用户记录(以查看他们的公共个人资料)
“ 我已经想到了几种不同的方式来组织和实现此功能,但我很好奇“Rails 方式”是做什么的。
URL 应该是不同的(domain.com/account 与domain.com/profile/someuser),这自然导致我希望有多个视图来表示此操作的不同观点。
选项一是创建两个具有不同显示操作和视图的控制器来服务请求。
选项二是将两个操作放置在一个控制器中,操作名称基本上是重复的(或强制不重复,例如“show”与“show_public”)。
选项三是使用一个控制器和一个操作,根据一些唯一的数据有条件地处理每个请求:调用它的路由、是否填充 params[:id] 等。
什么是“Rails Way”要这样做吗?
I have a User model that, in my mind, has two show actions:
- A 'show' performed on a user's own record (to view their private profile)
- A 'show' performed other user records (to view their public profile)
I've thought of several different ways I could organize and implement this functionality, but I'm curious what the 'Rails Way' is to do it.
The URLs should be distinct (domain.com/account vs. domain.com/profile/someuser), which naturally leads me to the desire to have multiple views to represent the different viewpoints of this action.
Option one would be to create two controllers with a different show action and view to serve the requests.
Option two would be to place both actions in one controller with action names that are essentially duplicates (or forced non-duplicates, such as 'show' vs. 'show_public').
Option three would be to use one controller and one action that processes each request conditionally based on some unique bit of data: the route from which it was called, whether or params[:id] is populated, etc.
What's the 'Rails Way' to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
任何用户的默认
路由
、控制器
方法和视图
,这里没有什么特别的。对于
current_user
:current_user
功能的身份验证 gem(例如 authlogic),route
,例如/me
current_user
来获取@userme.html.erb
视图来显示当前用户的具体信息希望有帮助
Default
routes
,controller
method andview
for any user, nothing special here.For
current_user
:current_user
featureroute
, e.g./me
current_user
to fetch @userme.html.erb
view to display current user specific informationHope it helps