# Class def error when serving "stand-alone" rhtml files

**URL:** <https://www.ruby-forum.com/t/class-def-error-when-serving-stand-alone-rhtml-files/100789>\
**Category:** Rails\
**Created:** [August 1, 2006, 2:31am UTC](https://www.ruby-forum.com/t/class-def-error-when-serving-stand-alone-rhtml-files/100789 "2006-08-01T02:31:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxbohr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f4b2a3/32.png) [@maxbohr](https://www.ruby-forum.com/u/maxbohr)\
**Post date:** [August 1, 2006, 2:31am UTC](https://www.ruby-forum.com/t/class-def-error-when-serving-stand-alone-rhtml-files/100789/1 "2006-08-01T02:31:47Z")

</div>

I’m attempting to set up a rails app so that I can add rhtml files  
without having to create controllers for each one. I want a simple  
solution to serving static-like rhtml files as well as those with a  
little logic. The solution I found is this one:

I added the following controller

static\_controller.rb  
#####################  
class StaticController \< ApplicationController  
end

I then created a folder named static in my views folder. All seemed  
fine(it worked) until I attempted to create a class in my rhtml code and  
the following error was thrown:

compile error  
script/…/config/…/app/views/static/imageMediation.rhtml:12: class  
definition in method body

I’m a bit of a noob if you can’t tell. Any help would be appreciated.  
Best

---

<div class="post-metadata">

**Author:** ![maxbohr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f4b2a3/32.png) [@maxbohr](https://www.ruby-forum.com/u/maxbohr)\
**Post date:** [August 1, 2006, 2:58am UTC](https://www.ruby-forum.com/t/class-def-error-when-serving-stand-alone-rhtml-files/100789/2 "2006-08-01T02:58:14Z")

</div>

the whole point of the controller is to interact between your views and  
your  
models. This may be why your view is giving you a hard time. If you’re  
serving out pages that don’t interact with the database at all, this is  
a  
good method, but as soon as you want to access a class, you should do  
that  
through the controller. I believe that when you don’t declare the  
action in  
the controller, you bypass the controller. That might have something to  
do  
with why you can’t access any models.

---

<div class="post-metadata">

**Author:** ![maxbohr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f4b2a3/32.png) [@maxbohr](https://www.ruby-forum.com/u/maxbohr)\
**Post date:** [August 1, 2006, 3:31am UTC](https://www.ruby-forum.com/t/class-def-error-when-serving-stand-alone-rhtml-files/100789/3 "2006-08-01T03:31:30Z")

</div>

Mike W. wrote:

> the whole point of the controller is to interact between your views and  
> your  
> models. This may be why your view is giving you a hard time. If you’re  
> serving out pages that don’t interact with the database at all, this is  
> a  
> good method, but as soon as you want to access a class, you should do  
> that  
> through the controller. I believe that when you don’t declare the  
> action in  
> the controller, you bypass the controller. That might have something to  
> do  
> with why you can’t access any models.

Right, I’m not trying to access the DB. The class is defined within the  
rhtml file like this for instance:

\<%  
class MyClass  
end  
%\>

blah

I get the error mentioned even if I don’t try to instantiate the class.

Is there some other(better) way to serve this sort of “one-shot” rhtml?

Thanks,  
max

---

<div class="post-metadata">

**Author:** ![maxbohr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f4b2a3/32.png) [@maxbohr](https://www.ruby-forum.com/u/maxbohr)\
**Post date:** [August 1, 2006, 3:48am UTC](https://www.ruby-forum.com/t/class-def-error-when-serving-stand-alone-rhtml-files/100789/4 "2006-08-01T03:48:39Z")

</div>

Hi –

On Tue, 1 Aug 2006, Max B. wrote:

> > the controller, you bypass the controller. That might have something to
> 
> blah
> 
> I get the error mentioned even if I don’t try to instantiate the class.
> 
> Is there some other(better) way to serve this sort of “one-shot” rhtml?

You should be able to circumvent the class definition in a method body  
error by doing this:

Object.const\_set(“MyClass”, Class.new { def x…; end })

But it does seem to cry out for a controller, or perhaps a class  
definition in a file in /lib (or maybe a helper file).

David

–  
[http://www.rubypowerandlight.com](http://www.rubypowerandlight.com) =\> Ruby/Rails training & consultancy  
----\> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! \<-----  
[http://dablog.rubypal.com](http://dablog.rubypal.com) =\> D[avid]A[.]B[lack’s][Web]log  
[Ruby for Rails](http://www.manning.com/black) =\> book, Ruby for Rails  
[http://www.rubycentral.org](http://www.rubycentral.org) =\> Ruby Central, Inc.

---

<div class="post-metadata">

**Author:** ![maxbohr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f4b2a3/32.png) [@maxbohr](https://www.ruby-forum.com/u/maxbohr)\
**Post date:** [August 1, 2006, 8:37pm UTC](https://www.ruby-forum.com/t/class-def-error-when-serving-stand-alone-rhtml-files/100789/5 "2006-08-01T20:37:20Z")

</div>

unknown wrote:

> Hi –
> 
> On Tue, 1 Aug 2006, Max B. wrote:
> 
> > > the controller, you bypass the controller. That might have something to
> > 
> > blah
> > 
> > I get the error mentioned even if I don’t try to instantiate the class.
> > 
> > Is there some other(better) way to serve this sort of “one-shot” rhtml?
> 
> You should be able to circumvent the class definition in a method body  
> error by doing this:
> 
> Object.const\_set(“MyClass”, Class.new { def x…; end })
> 
> But it does seem to cry out for a controller, or perhaps a class  
> definition in a file in /lib (or maybe a helper file).
> 
> David
> 
> –  
> [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com) =\> Ruby/Rails training & consultancy  
> ----\> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! \<-----  
> [http://dablog.rubypal.com](http://dablog.rubypal.com) =\> D[avid]A[.]B[lack’s][Web]log  
> [Ruby for Rails](http://www.manning.com/black) =\> book, Ruby for Rails  
> [http://www.rubycentral.org](http://www.rubycentral.org) =\> Ruby Central, Inc.

Thanks,  
Sticking the classes in /lib works great.  
Best,  
Max
