简体   繁体   中英

I wish to display a "show" action for two different models at the same; how do I go about this?

So I am making a little chat application to practice Ruby and Rails, and the way I want the actual chat view to work is basically like Discord, if you're familiar with that: a list of chats you are a part of on the left in a scrollable list, and on the right is the messages from the chat that you have selected, so you can click on the name of a chat on the left, and the messages appear on the right.

I have set it up so that chats and messages are separate models; the chat model just has an ID, a name and a list of participants, while a Message has its own ID, the ID (basically) of the chat it belongs to, the sender's ID, the content, and the date/time it was sent.

My question is: how can I display an index of two different models at the same time? Like, as far as I have used Rails so far, there would be one route/action for showing the list of channels, and one for showing the messages for a given channel, each corresponding to a different method on a different Controller. But I want to display both actions using one route, essentially (say the route would be /chat).

Put it in the Index Method in your Controller.

def Index
   @chats = Chat.all #you'll normally have this Kind already in your method
   @messages = Message.all
end

Is that what you mean?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM