ruby on rails 3, render multiple views in one view - ruby-on-rails-3

I have a problem and I dont know how to solve it.
I have a user that log in a web site, and I identify them by session[:user_id] and user has a status page that is defined in user_controller and in status view.
So, I would like to make one page for admin, to see all the statuses from all users on one page, using already written controller and view.
Is that possible?
I could rewrite some of the code, so that I could call with params, like ?user_id=70 and then set session[:user_id]=params[:user_id], but it would be painful if I will have to rewrite whole statuses, beside i would have to maintain same code on 2 different places.
Thank you.
Dorijan

If you need more functionality in your controller, you can add more actions to it. You can also do that in a resourcefull way.
On the other hand it usually is best practice to keep controllers thin.
See: ActionController
In order to make your views reusable, you should use partials.
You could make a _user_status partial.html.erb and render a single partial for a user render all of them for an admin.
Checkout: Layouts and Rendering in Rails

Related

MVC Routes & Subfolders

I'm trying to figure out a way to customize a Route that will allow me to use a subfolder within a particular View folder.
I have a Controller (FinanceAdmin) and a View folder (\FinanceAdmin) which contains a number of Views. Within that view folder, I have a lot of stand alone chart Views (Chart1, Chart2...Chart50, etc...) which I include as Partials on various View pages. To clean things up in my file/organizational structure, I would like to set things up like this:
I know I can use Areas to separate different parts of my application but that's not really what I'm looking for. I want to be able to create a custom Route so that, in my controller, I can simply return:
return View(chartdata);
instead of
return View("~/Views/FinanceAdmin/Chart/_Chart1.cshtml",chartdata);
Is that possible with a generic route (so I don't have to create one for each file)? I'd rather not write a custom view engine just for this unique circumstance.
I am afraid that this is not possible with a route. The routing engine finishes his responsibility at the time he finds (or doesn't find) a controller action to be executed given some request url.
Resolving views is purely the responsibility of the view engine. So if the conventions built into the view engine you are using do not meet your specific requirements, customizing this view engine is the right way to go.

Render layout without a controller

I'm writing a Rails 3.2 app with backbone, and since I only need rails to render one page, I have no need for a controller to back the index page.
Is there a way to render the layout (application.html.erb) without a controller? I imagine it would be a configuration in the routes.rb file?
My first thought was to move it to index.html in the /public directory, but I need to take advantage of erb for javascript includes and CSRF helpers, etc.
I get that you don't need the controller to do anything, but Rails is "opinionated" software; it expects a controller and a view, because that is the way it was designed, and trying to work around that is going to give you a lot of trouble.
Just
create an empty controller class in /app/controllers/main_controller.rb
create an empty view file /app/views/main/index.html.erb
set up a route like :root => 'main#index'
Easy peasy.

activeadmin: how do I controle what header menu items show based on user

I would like to create some logic with in my Rails app that is using ActiveAdmin backend and I'm not sure if it's possible or not.
Basically I'm wanting to give each AdminUser a new property called client_id. When the user logs in I would like to filter what is shown to them in the header menu.
Each item in the header will all have a "client_id" so that I can filter them.
It's possible, but you'll be doing a bit of custom stuff to get that working.
I'd suggest you would be way better off just rolling your own admin setup. You're going to jump through hoops and do some hackery that doesn't feel right to make everything play nice.

Rails render partial (with action code)

Is there a way to render a partial view with accompanying code in rails?
For instance: I want to be able to create a partial view which will show the top 5 foobars on my site. This partial needs accompanying code to retrieve some foobars from the database, rank them according to an algorithm, and then output the view with the top 5.
I want to be able to include this partial on any page I fancy, preferably just by using something like
<%= render :action => "top_five_foobars" %>
Is this doable? I'm used to asp.net mvc where you can create an action that runs some code and returns a partial, but it seems like in rails it returns simply the template...
If google get you here, you might be looking for cells
Cells are view components for Rails. They are mini-controllers with
their own MVC stack, can invoke logic and render views. They bring
back OOP to Rails' view layer and make writing reusable portlets for
your applications fun.
You need something like a shopping cart, which appears on almost
every page of your app.
You wouldn't use a partial and a helper, would you?
It might not be the cleanest way, but what I did is that I created a helper method in the Application Controller that retrieves the top 5 foobars. Then I call this method in the views. I also cached the part of the view that shows the results.

Understanding Rails helpers

I have two helper files. events_helper.rb and users_helper.rb.
Both of these helper files have a method called foobar. In events controller, index view. If I call foobar. Shouldn't it load the helper foobar thats in events_helper.rb?
Or is this not the way helpers work?
Seems like all helpers are available - so not sure which it would choose in your case, ideally the events controller one... and from your comment, it seems like its chosen the wrong one.
Could you give them distinct names?
Why are all Rails helpers available to all views, all the time? Is there a way to disable this?