# Problems creating a Controller Plugin

**URL:** <https://www.ruby-forum.com/t/problems-creating-a-controller-plugin/124686>\
**Category:** Rails\
**Created:** [January 1, 2008, 4:13am UTC](https://www.ruby-forum.com/t/problems-creating-a-controller-plugin/124686 "2008-01-01T04:13:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mozmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Mozmonkey](https://www.ruby-forum.com/u/Mozmonkey)\
**Post date:** [January 1, 2008, 4:13am UTC](https://www.ruby-forum.com/t/problems-creating-a-controller-plugin/124686/1 "2008-01-01T04:13:42Z")

</div>

I’ve created a plugin to generate error pages with a single function  
call. The basic idea is that in your action you could call  
error\_forbidden and an error page would be rendered with the HTTP  
status code 402. This works great so far when called from within an  
action, but when I want to add the call to the “verfy” line, things  
break:

verify :method =\> :post, :only =\>  
[:create, :update, :destroy], :render =\> { :text =\> error\_invalid }

Generates the error message: “undefined method `error\_invalid’ for  
EventsController:Class”

I tried using “def self.included(base)” with “module ClassMethods”,  
which seemed to work except I got an error about “render” not being  
defined.

How can I make the methods visible to the controller outside the  
actions? For some reason I haven’t been able to make it work for  
both. Here’s the plugin:

####### init.rb #######  
require ‘error\_page’  
ActionController::Base.send :include, ErrorPagePlugin

####### error\_page.rb #######

# ErrorPage

module ErrorPagePlugin

```
    #
    # The user is forbidden to perform this action
    #
    def error_forbidden(options={})
            options[:title] = options[:title] || "You do not have

```

access to  
perform that action."  
options[:message] = options[:message] || “Make sure  
you’re logged in  
and have access to this feature.”  
options[:template] = “errors/forbidden.rhtml”

```
            error_page(403, options)
    end

    #
    # Invalid form method (get, put, delete, post)
    #
    def error_invalid(options={})
            options[:title] = options[:title] || "Invalid Action"
            options[:message] = options[:message] || "You have

```

just tried to  
perform an action that is invalid for that item."  
options[:template] = “errors/invalid.rhtml”

```
            error_page(501, options)
    end

    #
    # Generate error page
    #
    def error_page(status=200, options={})
            @status = status
            @title = options[:title]
            @message = options[:message]

            @override_h1 = true
            @page_id = "pg_error"

            tmpl = options[:template] || "errors/generic.rhtml"
            render :status => status, :template => tmpl
    end

```

end

Thanks,  
Jeremy

---

<div class="post-metadata">

**Author:** ![Mozmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Mozmonkey](https://www.ruby-forum.com/u/Mozmonkey)\
**Post date:** [January 2, 2008, 8:55am UTC](https://www.ruby-forum.com/t/problems-creating-a-controller-plugin/124686/2 "2008-01-02T08:55:32Z")

</div>

Do anybody have any thoughts on this?
