[英]Ruby on Rails list table from different view
Is there a way to list (index) a table from a different unrelated view? 有没有办法从另一个不相关的视图中列出(索引)表? I have a Calendar view for using jquery Fullcalendar and an Events table. 我有一个使用jquery Fullcalendar的Calendar视图和一个Events表。 The Calendar doesn't have a model, so I can't relate it to Events. 日历没有模型,因此我无法将其与事件相关联。
Using the same code from the Events/index doesn't work: 使用事件/索引中的相同代码不起作用:
<% @events.each do |event| %>
or 要么
<% Events.each do |event| %>
I get: 我得到:
You have a nil object when you didn't expect it!
In any view you can show anything you want from any of your database tables, you simply need to pass it to the correct action in the controller. 在任何视图中,您都可以显示任何数据库表中的任何内容,只需将其传递给控制器中的正确操作即可。
So in whatever controller/action is displaying the Calendar view, add a @events = Event.all
(or narrow the search as you like). 因此,在显示日历视图的任何控制器/操作中,都添加一个@events = Event.all
(或根据需要缩小搜索范围)。 Then this instance variable becomes accessible in the views, and you can call it like you did: 然后可以在视图中访问此实例变量,然后可以像调用它一样进行调用:
<% @events.each do |event| %>
From what i understand by your question, you need to drop your calendar view into a partial that you can call and use elsewhere, in other views in your application. 根据我对您的问题的理解,您需要将日历视图放到可以在应用程序的其他视图中调用和使用的部分视图中。 The Rails Guide on Layouts and Rendering can help you with this. 关于布局和渲染的Rails指南可以帮助您解决这一问题。
Something like this in your view, is what you're after: 您所追求的是这样的事情:
<%= render :partial => "/events/calendar" %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.