[英]Rails: Combining Multiple Controller Actions with their own Views into a Single View
I have two different controllers with their own feed(index) actions and I want them to be displayed on a single page (home). 我有两个具有各自的feed(index)动作的控制器,我希望它们显示在单个页面(主页)上。 The posts and requests models are not similar and hold different types of data (images, video links, strings, etc.). 帖子和请求模型并不相似,并且包含不同类型的数据(图像,视频链接,字符串等)。
In my posts controller I have this action. 在我的帖子控制器中,我有此操作。 It uses two API actions in the same controller and fills a @post_array with specified record information which then gets displayed in the posts/feed.html.erb view: 它在同一控制器中使用两个API操作,并在@post_array中填充指定的记录信息,然后将其显示在posts / feed.html.erb视图中:
def feed
if user_signed_in?
post_feed_authenticated
else
post_feed_not_authenticated
end
end
In my requests controller I have a similar action that returns a @request_array with all relevant request records and displays them in requests/feed.html.erb. 在我的请求控制器中,我有一个类似的操作,该操作返回一个带有所有相关请求记录的@request_array并将它们显示在request / feed.html.erb中。
def feed
request_feed
end
The methods the actions call filter all records by location, authentication, and IDs. 动作调用的方法按位置,身份验证和ID筛选所有记录。
I've already created separate views for these actions that work correctly, but I want to combine them so both feeds are shown on the same page (home.html.erb). 我已经为这些可以正常工作的操作创建了单独的视图,但是我想将它们组合在一起,以便两个提要都显示在同一页面(home.html.erb)上。 How do I go about doing this? 我该怎么做呢? I've read into using partials but I am not sure if that is applicable here since the data is different. 我已经读过使用partials,但由于数据不同,因此不确定在这里是否适用。
Edit: The home controller is: 编辑:家庭控制器是:
def home_page
end
The home_page.html.erb currently has buttons to the corresponding post/request feed. home_page.html.erb当前具有相应的发布/请求提要的按钮。 I want to put both feeds on the home_page. 我想将两个提要都放在home_page上。
You can create instances of each class in the index action of the home controller: 您可以在home控制器的index操作中创建每个类的实例:
@posts = Post.all
@requests = Requests.all
Then you have access to all variables and methods in each class. 然后,您可以访问每个类中的所有变量和方法。
(Change index from above to whatever you named your home page.) (将索引从上方更改为您命名的首页。)
As I understand it - you want to reuse both the views and the code that populates @post_array
and @request_array
in your home view. 据我了解-您想重用视图以及在主视图中填充@post_array
和@request_array
的代码。 I see these as two different concerns with different solutions: 我将这些视为具有不同解决方案的两个不同的关注点:
home
view. 通过使用帖子供稿和请求供稿的局部视图,您可以在home
视图中重用该视图。 @post_array
you could try and place that logic in somewhere where your HomeController
has access to, ideally your Post
model (ie Post.feed(current_user, location)
) or a service object. 为了重新使用填充@post_array
的方式,您可以尝试将该逻辑放置在HomeController
可以访问的位置,最好是Post
模型(即Post.feed(current_user, location)
)或服务对象。 Another, hacky way to do this is by placing these methods in the ApplicationController
, making them available in all controllers. 另一种方法是通过将这些方法放在ApplicationController
,使它们在所有控制器中可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.