简体   繁体   English

如何在Rails 4中为一个表创建多个视图

[英]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.

相关问题 如何在Rails插件中测试视图? - How can I test views in a Rails plugin? Rails:对基础表进行更改后,如何重新生成脚手架创建的所有视图? - Rails: How can I regenerate all the views created by scaffold after I make changes to the underlying table? 如何将SQL Server表视图用作Rails模型(只读)? - How can I use SQL Server Table Views as Rails Models (Read Only)? 我如何找到帮助方法来在Rails视图之外创建html标签 - How can I find helper methods to create html tags outside of views in rails Rails:如何在不更改视图的情况下调用控制器的create动作? - Rails: How can I call a controller's create action without changing views? 如何将ActiveRecord Create用于Ruby on Rails的多个记录? - how can I use ActiveRecord Create for multiple records Ruby on Rails? 如何从Rails中的多个表关联中获取数据? - How can I get data from multiple table association in rails? 如何在Rails 3.1中以单个形式创建多个对象? - How can I create multiple objects in a single form in Rails 3.1? 如何在Rails 3中将多个属性传递给find_or_create_by? - How can I pass multiple attributes to find_or_create_by in Rails 3? 如何在 rails 上的多个表中创建关系? - How can I create relationships in multiple tables on rails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM