2

I've got an index page where I've decided to render both Usernames, Posts and other informative sources. Since Devise takes care of the user creations and login sessions-managing I am aware of needing to have multiple controllers.

PostsController, DashboardController and RandomController. The plan is to make them be rendered on different parts of the page, with different sizes and on different places.

Let's say I've got Post.find(:all => :order => "created_at DESC") in the PostsController. I want to render that someplace on the page. I want to render Usernames right under the according Post which belongs_to them, and I want to render some still "Random" Controller action which will post news on some random place on the page.

So a recap:

The PostsController finds and renders some part of the index page, and the DeviseControllers I guess renders the username belonging to the according one, and the Random Controller renders some random thing from news or something on the middle of the page. Amongst all of them all of this is happening on another controller called DashBoard. So all these controller actions are supposed to be rendered on a own DashBoard controller index view.

Any tips, or good ideas on how to accomplish this. I know about partial rendering, but I don't know how to achieve that.

Marcelo De Polli
  • 28,123
  • 4
  • 37
  • 47

1 Answers1

1

It seems we're talking about a view rendered by Dashboard#index. So all you need to do is define all the info on the page as instance variables under the index action.

Probably the bit of info that's missing for you is that controllers are free to use data from any of your models (and even other classes that don't inherit from ActiveRecord::Base).

For instance, in Dashboard#index, you can have:

@posts = Post.order("created_at DESC").all

Which is referring to a different model than the Dashboard model (assuming one exists).

Marcelo De Polli
  • 28,123
  • 4
  • 37
  • 47
  • So basically use all the actions I need from the other different controllers inside the index action of DashBoardController? – HotChickFromAlaska Jan 25 '13 at 22:01
  • The thing is, all that info is probably on models, not on controllers. So all you need to do is refer to those models from within your index action. – Marcelo De Polli Jan 25 '13 at 22:02
  • And no, you can't use actions from other controllers from within your controller. – Marcelo De Polli Jan 25 '13 at 22:03
  • Inside the models, is that the right place to keep them. You sure? Not like inside the helpers? Aren't the helpers more fitted to that? – HotChickFromAlaska Jan 25 '13 at 22:05
  • But you can with models and helpers? Please give me a brief summary here of what would be the best thing to do? – HotChickFromAlaska Jan 25 '13 at 22:06
  • Standard helper methods are not available to controllers, unless you slap them with a `helper_method`. So I think it's pretty safe to assume most of your data will be in models. – Marcelo De Polli Jan 25 '13 at 22:10
  • For instance, I know you have the `Post` model and, since you're using Devise, you must have a model storing your users. As for the random things, I cannot say. :) – Marcelo De Polli Jan 25 '13 at 22:11
  • Because that's the advantage of MVC! Even if you're only referring to instance variables in your views, that's still a lot better than referring directly to your Ruby classes from the point of view of organizing your code. – Marcelo De Polli Jan 25 '13 at 22:17
  • Last comment wasn't thought through- had to ditch it. Anyway...I guess models are the way to, but one last question, you see that @posts = Post.find code above, how would you write it to appear in the index dashboard view page? – HotChickFromAlaska Jan 25 '13 at 22:17
  • Just like this

    <%= @posts %>

    – HotChickFromAlaska Jan 25 '13 at 22:20