使用多个模型数据创建视图
我的(rails 3)应用程序使用 collection
和 product
作为模型。集合 has_many
产品和产品 belongs_to
集合。
我设法在产品和模型之间进行交互。我创建了一个显示不同集合的菜单。我想显示一个仅显示属于特定集合的产品的视图。
1)创建一个新的视图/控制器更优雅,还是在产品视图中创建一个新视图
2)似乎我必须对routes.rb做一些事情,但是如何做以及做什么?
3)我必须使用什么 link_to 参数来传递我的集合的值?
4)我读了一整本书(务实的ROR),尽管如此,并且做了很多在线研究,我仍然在这里结束询问不太复杂的Rails问题。我做错了什么?
My (rails 3) application use collection
and product
as models. collection has_many
products and product belongs_to
collection.
I managed to have interactions between products and models. I created a menu displaying the different collection. I want to display a view showing only the product belonging to a specific collection.
1) Is it more elegant to create a new view/controller, or do i create a new view in the product views
2)It seems that i must do something with the routes.rb, but how and what?
3)What link_to arguments must i use to pass the value of my collection?
4)I read a whole book (pragmatic ROR) and depspite that and doing a lot of online research i keep ending here asking for not so much complicated Rails question. What am i doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会在集合控制器中创建新操作。网址将如下所示:
/collections/1/产品
其中 1 是集合 id。
我假设你有
资源合集
因此您需要为集合成员添加“产品”操作:
您可以从控制台运行 rake 路由来查看应用程序路由现在的外观。
链接代码应如下所示
在我看来,阅读是可以的,但是当你阅读时,你应该做很多例子,自己写,因为否则你很快就会忘记东西。我 100% 确定我上面写的东西在你读过的书中。
I would go with creating new action in collection controllers. Url will look like this:
/collections/1/products
where 1 is collection id.
I assume you have
resources collections
so you need to add 'products' action for collection member:
You can run rake routes from console to see how your application routes look now.
Link code should look like this
In my opinion reading is ok, but while you read you should do lots of examples, write them yourself, becouse otherwise you forget stuff very quickly. I'm 100% sure that stuff I wrote above was in the book you've read.