Confused About the MVC Flow

Ok so get '/messages' => 'messages#index' calls the “index” method inside the “messages” controller when that route is reached. In that method, we are assigning @messages = Message.all which assigns all messages to that variable (is it called a variable or is there another proper name?). Where I got lost is when did we associate a view with a url? I.e. How does this app know to display the messages by using index.html.erb when messages#index was called?

Hi @microjumper04253,

Rails will look for a file with the same name as the action. So, if you have a route like get '/random_url => 'pages#random_url' (for example), Rails would look for a random_url.html.erb file in the app/views/pages/ directory.

Thank you for clarifying!

1 Like

This topic is solved.