Reputation: 3520
i am new to RoR and I am a little confused on this topic. Is the scope of variables in the controller only accessible in it's corresponding view?
for instance say I generated a Post scaffold (with blog_text and title). I also generated a controller called static with a home page view/controller resource. Is there any way I can access/print all of the posts from within my home page view? would i have to do something in my static#home function? I cant jsut do Post.all correct?
Upvotes: 4
Views: 4135
Reputation: 4566
The controllers are tied to their corresponding views. But you need to use an instance variable (a variable with '@' in the front e.g. @variable
) if you want to be able to use the variable in your views. Also, it doesn't matter what model/view/controller you're in when you request data from your db. So in response to your question, yes you can just do something like
@posts = Post.all
in any controller and then access the posts in your views. This is pretty basic stuff, you should study the guide a bit more.
Upvotes: 3