开发者

Rails 3.1 Engine: How do I get the engine application_controller to talk to the client app's application_controller?

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开发者_如何学Python 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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜