简体   繁体   English

Ruby on Rails列表从不同的视图

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

相关问题 Ruby on Rails从视图访问关联表 - ruby on rails access associated table from view 从带有回形针宝石红宝石的视图中访问不同视图中的图像 - Access image from different view in a view with paperclip gem ruby on rails Ruby on Rails:如何从相同的控制器渲染具有不同路线的视图? - Ruby on Rails: how to render a view from same controller but with a different route? 视图未显示Ruby On Rails中关联表中的数据 - View not showing data from associated table in Ruby On Rails 如何从默认视图更新连接表中的数据? - 导轨上的 Ruby - How to update data in a joined table from a default view? - Ruby on Rails Ruby on Rails,来自其他表的Enum用户条件 - Ruby on Rails, Enum user conditions from a different table 尝试引用其他表中的列时出现NoMethodError-Ruby on Rails - NoMethodError when trying to reference a column from a different table - Ruby on Rails 从搜索结果中显示带有 Ruby on Rails 的列表/或表格 - Display list/or table with Ruby on Rails from search result Ruby on rails:需要帮助将两个不同循环和两个不同部分的视图格式化为一个视图 - Ruby on rails: Need help formatting view from two different loops and two different partials into one view 从Rails 4中的其他控制器/视图将数据保存在表中 - Save data in table from different controller/view in Rails 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM