[英]How can I create multiple views for a table in Rails 4
I have a single table in which I store favourite dishes of users. 我有一张桌子,用来存放用户喜欢的菜肴。 A user can have more than one entries in the table.
一个用户在表中可以有多个条目。 I have generated a view to show all the entries in the table but how do I create a view to show just the list of users.
我已经生成了一个视图以显示表中的所有条目,但是如何创建一个视图以仅显示用户列表。 I don't have a table saving the users so I have to use DISTINCT to get the names of the users from my one and only table
我没有保存用户的表,因此必须使用DISTINCT从我唯一的表中获取用户名
Sounds like you have a lot to learn about relational databases, schema design and data modeling so this answer might not help very much but here goes... 听起来您对关系数据库,模式设计和数据建模有很多了解,所以这个答案可能无济于事,但是这里...
You're at a point where you should normalize your schema and put users in their own table. 您现在应该规范化架构并将用户放在他们自己的表中。 Then the dishes table would have a
user_id
field pointing back to the id
of the user it belongs_to
. 然后菜表将有
user_id
场指回id
它的用户的belongs_to
。
So, you'll have a User model that has_many
Dishes. 因此,您将拥有一个
has_many
Dishes的用户模型。 And a Dish model that belongs_to
User. 以及
belongs_to
用户的Dish模型。 With that you'll more easily be able to create views for users without having to do a slightly complex query to get that data out of the Dish table. 这样一来,您就可以更轻松地为用户创建视图,而不必执行稍微复杂的查询即可从Dish表中获取该数据。
My routes had to be fixed and it worked .. 我的路线必须固定并且有效..
The default route.rb looks like this 默认的route.rb看起来像这样
ActionController::Routing::Routes.draw do |map|
ActionController :: Routing :: Routes.draw做| map |
# The priority is based upon order of creation#优先级基于创建顺序
# First created gets highest priority.#首先创建具有最高优先级。
# Sample of regular route: # map.connect 'products/:id', :controller => 'catalog', :action => 'view' # Keep in mind you can assign values other than # :controller and :action
#常规路线示例:#map.connect'products /:id',:controller =>'catalog',:action =>'view'#请记住,您可以分配除#:controller和:action外的其他值
# Sample of named route: # map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase' # This route can be invoked with purchase_url(:id => product.id)
#命名路线的示例:#map.purchase'products /:id / purchase',:controller =>'catalog',:action =>'purchase'#此路线可以通过purchase_url(:id => product.id调用) )
# You can have the root of your site routed by hooking up '' # -- just remember to delete public/index.html.
#您可以通过连接''#来路由站点的根目录-仅记住删除public / index.html。 # map.connect '', :controller => "welcome"
#map.connect'',:controller =>“欢迎”
# Allow downloading Web Service WSDL as a file with an extension
#允许将Web Service WSDL下载为带有扩展名的文件
# instead of a file named 'wsdl' map.connect ':controller/service.wsdl', :action => 'wsdl'
#而不是名为'wsdl'的文件map.connect':controller / service.wsdl',:action =>'wsdl'
# Install the default route as the lowest priority.
#将默认路由设置为最低优先级。 map.connect ':controller/:action/:id.:format' map.connect ':controller/:action/:id' end
map.connect':controller /:action /:id。:format'map.connect':controller /:action /:id'结尾
I found this tutorial helpfull http://www.informit.com/articles/article.aspx?p=1087656&seqNum=5 我发现本教程很有帮助http://www.informit.com/articles/article.aspx?p=1087656&seqNum=5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.