Menu displayed on all pages, code is replicated in all controllers - ruby-on-rails

I have a collection model. I succesfully created a _collection.html.erb that i call with <%= render #collections%> in my application layout.
My problem is that in ALL my controlers method I must add #collections = Collection.all
I found it very very ugly,it will make my collection scope a pain to change, and I'm sure that I am missing a rails magic something that would be way nicer.
Is there a way to have a part of the layout generated by model data without having a identical piece of code in AAAALLLLLL the controllers?

Notice that your controllers all inherit from ApplicationController. Use this to your advantage. Add a before_filter to ApplicationController that loads your collections.

#cam was right. Any rails project has an ApplicationController. Your controllers all start with MyController < ApplicationController, right? If so, that means you can create a before_filter in your ApplicationController, which will be inherited by all your controllers. To do so :
/app/controllers/application_controller.rb
before_filter :load_collection
def load_collection
#collections = Collection.all
end
From now on, you can use #collections from all your controllers (as long as they inherited from ApplicationController)

Related

Share the same piece of data in multiple controllers and views

Lets say that we have a rails app which has a sidebar on every page that shows some data, like post archives, post categories etc. Which is the best way to share the same data on each of our controllers?
Iguess the most easy fix is to use the same before_actions on each controller, but this doesn't DRY up much our code, or maybe move all these into a parent class/cotrnoller that all controllers will inhert from, but is there a better way of doing this?
Simple Solution:
Normally, I just put these into application_controller.rb as a before_action.
Example
# app/controllers/application_controller.rb
before_action :set_sidebar_resources
# ...
private
def set_sidebar_resources
#sidebar_archives = Archive.all
#sidebar_categories = Category.all
end
Modular Solution:
Simple solution above works great until you define more and more methods and other global controller logic into ApplicationController, and then the file becomes too big to manage. The following is a less conventional approach favouring more of manageability rather than simplicity.
Example
# app/controllers/application_controller.rb
include WithSidebar
# app/controllers/concerns/with_sidebar.rb
module WithSidebar
extend ActiveModel::Concern
included do
before_action :set_sidebar_resources
private
def set_sidebar_resources
#sidebar_archives = Archive.all
#sidebar_categories = Category.all
end
end
end
Rails controllers inherit from application controller.
ex.
class ClientsController < ApplicationController
You could add before_actoion to ApplicationController that pulls the data you want into #variable and then use that variable in other controllers.
Just don't over-do it and keep your controllers skinny :)

Rails where do global variables go?

I'm learning alot about the DRY concept in Rails so here's my question:
I have a fixed sidebar in my application that always needs to load various Objects, such as Locations.
In the controller related to my index page, actually in controllers/pages_controller.rb I'm loading #locations:
#company = current_user.company
#locations = if #company
current_user.company.locations.order(:name)
else
[]
end
Which works fine for my navigation menu (sidebar), however, once I select a location and load a view delegated from a different controller (in this case locations/show.html.erb) my #locations variable is no longer found.
Sure, I could add it to the show method in my locations_controller.rb file, but that isn't very DRY considering I'd have to put the above code in every single controller method that relies on the sidebar navigation menu (all of them!) since it's fixed.
What's the DRY way to do this once and not have to do it in every controller?
You can create a method in your application_controller
def set_locations
....
end
Then in your pages_controller and locations_controller add a before_filter
before_filter :set_locations
It will set call set location before all all methods of the pages and locations controller.
If you want to set locations only for few controller actions then call:
before_filter :set_locations,:only=>[:show,:index, :your_action_name]
Updated:
In Rails 5 before_filter is deprecated and you can use before_action
You could add it to the ApplicationController in a method that runs before every action, every controller inherits from this ApplicationController so it will be available.

Keep Controllers DRY : where to put shared methods?

My code is starting to have duplicates.... because I don't know where to put methods/functions that need to be accessed by (2) different controllers, and just to get it up quickly I just duplicated it.
So I have a users_controller, and a pages_controller and posts_controller.
On the users page, I have posts being displayed.
On the homepage, I also have posts being displayed.
Where should I put shared/common code so that the users_controller and pages_controller and posts_controller can all have access to ithese methods?
Right now, I have duplicated def methods in some controllers, and also some duplicate private methods in each controller.
On a related note, how can I access methods defined in one controller from another controller?
Maybe a module?
module ProductSharedMethods
def product_list
Product.scoped
end
end
class UsersController < ApplicationController
include ProductSharedMethods
def index
#products = product_list
end
end
If you have posts being displayed on two separate pages then use a partial. I don't know what version of rails you are using but newer versions have a views/application folder for shared partials.
If you have a method that is shared across more than one controller then put it in the application controller. Controllers are classes so follow all of the normal rules.
Hope that helps.

rails 3: layout for namespaced routes

I've created a number of controllers & views under an 'admin' namespace, but they are still pulling from the application layout. how can I make a layout that applies to all the views in the namespaced routes, and still use the current layout for the other pages?
I usually have a Base controller class in my namespace, and then have all controllers in that namespace inherit from it. That allows me to put common, namespace specific code in Base and all the controllers in that namespace can take advantage. For example:
class Admin::BaseController < ApplicationController
layout 'admin'
before_filter :require_admin_user
end
class Admin::WidgetsController < Admin::BaseController
# inherits the 'admin' layout and requires an admin user
end
Generally speaking, Rails will use the application layout if there isn't a layout that matches the controller. For example, if you had a PeopleController, Rails would look for layouts/people.html.erb and if it didn't find that, application.html.erb.
You can explicitly specify a specific layout if you want to override this convention.
class Admin::PeopleController
layout 'some_layout'
end
That controller will then use some_layout.html.erb rather than looking for people.html.erb and application.html.erb.
But this might be a better way if you're looking to group things: If you have a base AdminController that inherits from ApplicationController, you can have your, say, Admin::PersonController inherit from the AdminController and it will inherit the admin layout.
I don't know the specifics of your code, but you might have:
class AdminController
def show
#render a template linking to all the admin stuff
end
end
app/controllers/admin/people_controller.rb:
class Admin::PeopleController < AdminController
#your awesome restful actions in here!
end
views/layouts/admin.html.erb:
Hello from the Admin!
<%= yield %>
The one thing to realize is that Admin::PeopleController will inherit any actions that AdminController has defined (just as anything defined in ApplicationController becomes available in all sub-classes). This isn't generally a problem since you'll likely be overwriting the methods anyway, but just to be aware of it. If you don't have an AdminController, you can make one with no actions just for the purposes of the layout.

where to put controller code for partials you render from different views in Ruby on Rails

A newbie question:
I have a partial that I'm loading on a website on different pages.
That partial needs some data, so in the controller of the parent view I do the query:
#properties = Property.find(:all)
I pass the query results to the partial in the view using :locals
Now, I would like to render the same partial from another view. This view has a different controller. Do I have to repeat the query in that controller also? Or can I have a separate controller for partials I use one more places in the website. In this last case, how do I indicate which controller to use in the view when I put the render command?
Or should I use somethink different than partials for this kind of use?
Kind regards,
Michael
How i would solve this, is to define a new file inside your lib folder, with a meaningful name. For now i will just use does_as_shared.rb:
module DoesAsShared
def setup_shared_data
#shared_data = ...do something useful here ...
end
end
and then inside your controllers that need that code you write:
class ThingsController < ApplicationController
include DoesAsShared
def index
setup_shared_data
.. and do some more stuff ...
end
end
The advantage of this code is that the shared code is limited to those controllers that really need it. And at the same time it is pretty readable: the include statement, given that the name is chosen well, clearly indicates what functionality is included.
You don't need to copy the code to set up the partial's data, but you would need to hoist it into a controller that your two controllers inherit from. Normally, this would be your ApplicationController. Define a method in there that loads the data you need. For example:
class ApplicationController < ActionController::Base
# ... your code ...
protected
def load_partial_data
#properties = Property.find(:all)
end
end
Now to actually call this method you have two options:
If all your controllers need this data, add a before_filter to your ApplicationController like before_filter :load_partial_data
Otherwise, add the before_filter to just the controllers that actually need to load the data.
I hope this helps.

Resources