Rails“路由”;基于模型属性
我试图根据当前经过身份验证的用户的属性在应用程序的主页上执行不同的操作。例如:
location, user, state, -> destination
/, no user -> a home page
/, user authenticated, state: unverified -> user profile page
/, user authenticated, state: verified -> a content listing
这看起来是我正在尝试根据当前用户的状态(由状态机表示)进行“路由”。这 3 个操作已经存在于 3 个不同的控制器中(我称它们为“页面”、“用户”和“帖子”),但是虽然一个可以调用另一个控制器的视图,但不能调用另一个控制器的操作,这使得它有点困难为了不重复自己。有很多方法可以解决这个问题,但我不确定 Rails Way 的用途是什么,所以我想我应该问一下。我看到我的选择:
- 在假设的“重定向控制器”中使用redirect_to,但我希望页面显示在/下,所以这不是我想要的。
- 喜欢路由约束(不确定这是否可能;需要路由中可用的会话/cookie,我不确定情况是否如此)
- 从各自的控制器中提取特定操作的逻辑,将它们扔到 ApplicationController 中,然后使用它们直接基于用户在假设的控制器中的状态(或者只是将其扔到页面中)。
- 重要地重复我自己,无论是在控制器、视图还是在两个
- 尚未知的选项中,我都愿意接受建议。
我倾向于第三种选择,其明显的缺点是这些控制器中的某些部分现在或多或少莫名其妙地存在于 ApplicationController 中(除非上帝帮助我,我做了某种洛夫克拉夫特式的包含扩展)。让这段代码存在于两个地方对我来说感觉很脏。
我错过了一些明显的事情吗?
I am trying to do different things on the home page of my application based on properties of the currently authenticated user. For example:
location, user, state, -> destination
/, no user -> a home page
/, user authenticated, state: unverified -> user profile page
/, user authenticated, state: verified -> a content listing
What this looks like is that I am trying to 'route' based on the current user's state (as represented by a state machine). These 3 actions already exist in 3 different controllers (I call them 'pages', 'users', and 'posts'), but while one can call another controller's view, one can't call another controller's action, making it a little tough to not repeat myself. There's a number of ways to deal with this, but I'm not sure what The Rails Way is for this, so I thought I'd ask. I see as my options:
- Use redirect_to in a hypothetical 'redirect controller', but I want the page to appear under /, so this isn't what I want.
- Get fancy with a routing constraint (not sure this is possible; need sessions/cookies available in routing and I'm not sure that's the case)
- Pull the logic for the particular actions out of their respective controllers, toss them into ApplicationController, and use them directly based on the user's state in a hypothetical controller (or just toss it into pages).
- Repeat myself significantly, either in the controller, the views, or both
- Yet-unknown options, I'm open to suggestions.
I'm leaning towards the third option, with the obvious downside that some piece of those controllers will now more or less inexplicably live in the ApplicationController (unless, god help me, I do some sort of Lovecraftian include-on-extend). Having this code live in two places feels dirty to me.
Am I missing something obvious?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用助手根据用户当前状态选择正确部分的单个操作是否有效?
另外,看看使用 ActiveSupport::Concern 而不是使用所有洛夫克拉夫特式的包含扩展。 http://api.rubyonrails.org/classes/ActiveSupport/Concern.html
Would a single action that uses a helper to pick the right partial based on the current state of the user work?
Also, take a look at using ActiveSupport::Concern instead of getting all Lovecraftian include-on-extend. http://api.rubyonrails.org/classes/ActiveSupport/Concern.html