ruby-on-railsrubyruby-on-rails-3.2alchemy-cms

Creating a custom Guestbook Module for Alchemy CMS


I'm trying to build a guestbook module within a Rails site using the Alchemy CMS framework. There doesn't appear to be much in the way of documentation for module building with Alchemy, so I'm just going off of this page.

I've created two controllers, one that admins will use called guestbook_controller.rb and placed this under app/controllers/admin

module Admin
  class GuestbookController < Alchemy::Admin::ResourcesController
    def index
      "index"
    end
  end
end

and another for guest to access under app/controllers/guestbook_controller.rb

class GuestbookController < ActionController::Base
  def index
    "index"
  end
end

My intention is that Guestbook posts will be displayed under one of the pages already within Alchemy and a form will also be displayed on this page.

The guestbook model looks this:

class GuestbookEntry < ActiveRecord::Base
  attr_accessible :location, :message, :name
end

My routes file looks like the following:

resources :guestbook

namespace :admin do
  resources :guestbook
end

mount Alchemy::Engine => '/'

and I have a file called authorization_rules.rb under config that looks like: authorization do

  role :admin do
    has_permission_on :guestbook, :to => [:manage]
  end

end

The first problem that I'm encountering is that going to the route /admin/guestbook gives me the error 'You are not Authorized', but the authorization rules file should be being called by my initalizer, so why am I getting this error?

# Registering guestbook module in Alchemy CMS
Alchemy::Modules.register_module(YAML.load_file(File.join(File.dirname(__FILE__), '../..', 'config/guestbook_module.yml')))

# Loading authorization rules and register them to auth engine instance
Alchemy::AuthEngine.get_instance.load(File.join(File.dirname(__FILE__), '../..', 'config/authorization_rules.rb'))

Solution

  • The problem with the authorization is easy. You just need to write:

    has_permission_on :admin_guestbook, :to => [:manage]
    

    Another thing I noticed: Your frontend GuestbookController should inherit from Alchemy::BaseController.

    And you should make sure that your page you want to render the guestbook entries on, must not be cached by Alchemy. You can do this by using cache: false option in the page_layouts.yml for your page layout.