Rails 3 具有用户(设计)结构的嵌套资源
我遇到了 Rails 3 的一般结构问题,而新的 paths.rb 让我有点困惑。感谢您的任何帮助或指导。
我有一个带有嵌套资源的论坛应用程序。有部分、主题和回复。 routes.rb 结构如下所示:
resources :sections do
resources :topics do
resources :replies
end
end
我的section.rb:
has_many :topics
has_many :replies, :through => :topics
我的topic.rb:
belongs_to :section
has_many :replies
我的reply.rb:
belongs_to :topic
这工作得非常好。现在这就是我感到困惑的地方。
我使用 Devise 添加了一个用户控制器,并且有一个有效的用户名登录/注销系统。我正在尝试将“current_user”与回复和主题连接起来。我认为我对如何修复模型有一个好主意,但我对在 paths.rb 文件中做什么很困惑。
对于 user.rb,我相信我需要添加“has_many :topics”和“has_many :replies, :through => :topics”。然后在我的主题中我需要添加“belongs_to:user”。我相信reply.rb保持不变?
至于routes.rb,我有点难住了。如果我编辑路由并向其添加用户,我会得到像sectionid/username/topicid/这样的路径,但我不一定需要在这样的路由中存储用户名。那么我是否可以将用户嵌套在部分和主题之间,或者可以将用户排除在routes.rb 文件之外。
I'm having general structural issues with Rails 3 and the new routes.rb is getting me a bit confused. Thanks for any help or guidance.
I have a forum application with nested resources. There are sections, topics, and replies. The routes.rb structure looks like this:
resources :sections do
resources :topics do
resources :replies
end
end
My section.rb:
has_many :topics
has_many :replies, :through => :topics
My topic.rb:
belongs_to :section
has_many :replies
My reply.rb:
belongs_to :topic
And this is working wonderfully. Now here's where I'm confused.
I added a user controller using Devise, and have a working username login/logout system. I'm trying to connect the 'current_user' with replies and topics. I think I have a good idea on how to fix the models, but I'm very confused with what to do in the routes.rb file.
For user.rb, I believe I need to add "has_many :topics" and "has_many :replies, :through => :topics". And then In my topics I need to add "belongs_to :user". I believe reply.rb remains the same?
As for the routes.rb I'm kind of stumped. If I edit the routes and add users to it, I would get a path like sectionid/username/topicid/ but I don't necessarily need to store a username in a route like that. So do I nest user in-between sections and topics or can I leave user out of the routes.rb file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将用户排除在路径之外。只需将 devise_for :users 包含在路由顶部,而不将其包含在资源块中。
一个用户有很多话题,也有很多回复。主题和回复都属于用户。
您的控制器需要进行更多更改。您需要添加 before_filter 来检查用户是否经过身份验证,此外还需要更改大多数控制器方法来检查用户是否不仅经过身份验证而且还经过授权,例如,对于编辑查找,您需要执行 current_user.replies... 。您可以在这里阅读更多内容(我自己的页面):http://www.communityguides.eu/articles/ 4.。
You can leave the users out of the path. Just include the devise_for :users on top of your routes without including it in your resources block.
A user has many topics and has many replies. Both topics and replies belong to a user.
There are more changes in your controllers needed. You need to add a before_filter to check if users are authenticated and in addition changes in most of your controller methods to check if users are not only authenticated but also authorized, e.g. for an edit lookup you do current_user.replies.... . You can read more on that here (my own page): http://www.communityguides.eu/articles/4.