Why do we use instance variables in the Messages controller's index method?


#1

This is the code for the Messages controller in the SavingData#View section.

class MessagesController < ApplicationController

def index
@messages = Message.all
end
end

If I have understood correctly, the ‘@’ before ‘messages’ in the ‘index’ method says that ‘messages’ is an instance variable. My question is - why do we use an instance variable here when the real use for this variable is to set it in the view and no other method is going to use this variable at a different time.

Thanks


#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.