Max Benjamin
2006-Aug-01 00:32 UTC
[Rails] class def error when serving "stand-alone" rhtml files
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 -- Posted via http://www.ruby-forum.com/.
Mike Weber
2006-Aug-01 00:56 UTC
[Rails] class def error when serving "stand-alone" rhtml files
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. On 7/31/06, Max Benjamin <moore.joseph@gmail.com> wrote:> > 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Mike Weber Web Developer UW-Eau Claire -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060801/1268ca05/attachment.html
Max Benjamin
2006-Aug-01 01:31 UTC
[Rails] Re: class def error when serving "stand-alone" rhtml files
Mike Weber 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 %> <p>blah</p> 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 -- Posted via http://www.ruby-forum.com/.
dblack@wobblini.net
2006-Aug-01 01:45 UTC
[Rails] Re: class def error when serving "stand-alone" rhtml files
Hi -- On Tue, 1 Aug 2006, Max Benjamin wrote:> Mike Weber 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 > %> > <p>blah</p> > > > 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 => Ruby/Rails training & consultancy ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <----- http://dablog.rubypal.com => D[avid ]A[. ]B[lack''s][ Web]log http://www.manning.com/black => book, Ruby for Rails http://www.rubycentral.org => Ruby Central, Inc.
Max Benjamin
2006-Aug-01 18:37 UTC
[Rails] Re: Re: class def error when serving "stand-alone" rhtml fil
unknown wrote:> Hi -- > > On Tue, 1 Aug 2006, Max Benjamin wrote: > >>> the controller, you bypass the controller. That might have something to >> <p>blah</p> >> >> >> 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 => Ruby/Rails training & consultancy > ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <----- > http://dablog.rubypal.com => D[avid ]A[. ]B[lack''s][ Web]log > http://www.manning.com/black => book, Ruby for Rails > http://www.rubycentral.org => Ruby Central, Inc.Thanks, Sticking the classes in /lib works great. Best, Max -- Posted via http://www.ruby-forum.com/.