0

I have a new, mountable rails 3.1 engine and I need the client app, that being the rails app that will include this engine, to define a generic permission based method.

So, what I want is in my engine's blog controller to say something like:

before_filter :redirect_unless_admin

And then I want to leave it to the client app to define who is an admin. However, whenever I try this I get:

NameError in Blog::BlogsController#show

undefined local variable or method `redirect_unless_admin' for #<Blog::BlogsController:0x000001058aa038>

My client app controller looks something like this:

class ApplicationController < ActionController::Base

  # Required by blog engine
  def redirect_unless_admin
    if !logged_in?
      set_session_redirect
      redirect_to login_path, :notice => 'Please log in.'
    elsif !current_user.admin?
      set_session_redirect
      redirect_to root_path, :notice => 'You do not have permission to view this page.'
    end
  end

And in my engine app controller, I have the following:

module Blog
  class ApplicationController < ActionController::Base
  end
 end

Can somebody tell me how to set it up so that my engine's blog controller can talk to my client's application_controller?

tshepang
  • 12,111
  • 21
  • 91
  • 136
ynkr
  • 25,946
  • 4
  • 32
  • 30
  • I think answer from [this question](http://stackoverflow.com/questions/7323327/a-way-to-add-before-filter-from-engine-to-application/) is related to your situation. – Dmitry Maksimov Sep 19 '11 at 06:42
  • Dmitry, that post is the inverse of what I need. That ticket asks 'how do I make methods from the engine available to the application'. What I need to know is 'how do I make methods from the application available to the engine?'. Also, this is a 3.1 engine and app so my assumption was doing any kind of loading of helpers, views, assets, etc was taken care of for us (which is why this is so confusing for me). – ynkr Sep 19 '11 at 16:10

2 Answers2

4

The answer ended up being painful, and simple. In my engine, I have the following for my blogs controller:

module Blog
  class BlogsController < ApplicationController

I then took a look in my engine's application controller and saw this:

module Blog
  class ApplicationController < ActionController::Base

The problem was I wanted my engine to look at it's application_controller and then the main app's application_controller if nothing was found, so I changed to this and everything works great:

module Blog
  class ApplicationController < ::ApplicationController

If you know of a different/better/more elegant/best practice/whatever solution, I would love to hear it.

ynkr
  • 25,946
  • 4
  • 32
  • 30
0

you should look into

rails plugin new forum --full        # Engine
rails plugin new forum --mountable   # Mountable App

The engine is like an extension of the parent app. Thus you should be able to call the application helper_methods.

The mountable app is isolated from the parent app. I believe you are using a mountable app. Look into changing to a rails engine with the above commands.

drhenner
  • 2,200
  • 18
  • 23