Custom Front End Controllers

Jamel Toms edited this page Aug 15, 2014 · 2 revisions

Sometimes you just can't do what you want to do with content blocks and portlets. You may just want the standard rails index, show, create, etc. views, but without the admin interface.


First define a subclass of Cms::ContentBlockController. This will provide the right environment for your cms templates to render properly. It includes the core helper modules that define things like current_user as well as bring in the security and permissions model.

# app/controllers/custom_controller.rb
class CustomController < Cms::ContentBlockController

  # needed to allow view helpers to work properly (e.g. cms_toolbar)
  # comment out this line if you don't  
  include Cms::TemplateSupport

  # needed to get the menus to render for non-cms namespaced controllers
  before_filter :get_home_page

  layout 'templates/default'


  def get_home_page
   @page = Cms::Page.find_live_by_path("/") 


Then subclass this controller for each of your custom controllers and author them as you would any standard rails controller.

# app/controllers/bacon_chunks_controller.rb
class BaconChunksController < CustomController

  def index
    @bacon_chunks.all(:order => :crispiness)

  def show


In your views, you need to make sure you're putting everything in the right place by wrapping the content in a content_for block.

# app/views/bacon_chunks/index.html.erb
<%- content_for(:head_html) do -%>
<%- end -%>

<%- content_for(:main) do -%>
  <%= render :partial => 'bacon_chunk', :collection => @bacon_chunks %>
<%- end -%>

This is probably not necessary for a stock installation, but if you have some custom authentication you may run into any problems with the cms_toolbar forcing a redirect. I recommend modifying your layout with a wrapper for cms_toolbar:

# app/helpers/application_helper.rb
  def my_cms_toolbar
    return (current_user.guest?) ? '' : cms_toolbar


You need to define explicit routes to make this all work. BCMS has a catch all route that handles non-explicitly defined routes and it won't know about your new controller. Make sure your custom controller routes come before any routes defined for BCMS.

# config/routes.rb

  map.connect 'bacon_chunks/:action/:id', :controller => 'bacon_chunks_controller'
   # -->> bcms routes down here <<--

Final Thoughts

This will render your page with the toolbar if you're logged in, but it won't let you edit the page with the toolbar. You're working outside the CMS, so this behavior makes sense. If you need editing, you have to bake that into your controller and views yourself.

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.