开发者

Make all ActionViewer helper methods available in a module

I am using ruby on rails 3. I have a model called Content. Within the controller, I extract some variables and pass them to the Viewer. In the Viewer, I would like to use of my self made functions so that I don't have too much logic and code in the Viewer. I defined these functions in the ContentsHelper module.

My functions defined in the ContentsHelper module are making use of ActionViewer helper functions, however these are not available in the ContentsHelper module. I am getting for example NoMethodError: undefined method content_tag' for #<Content:0x1025b9458> error.

How is it possible to make all the ActionViewer helper functions available?

Newbie.

following suggestion of Tim:

module ContentsHelper
include ActionView::Helpers
  def bla
    content_tag(:h2, self.title)        
end

I can run the bla method as Content.bla in the console. This is perfect!

But when I replacec content_tag with image_tag, then I run into trouble and get this error:

NoMethodError: undefined method `config'
from /Users/sonat/.rvm/gems/ruby-1.8.7-p334/gems/activemodel-3.0.0/lib/active_model/attribute_methods.rb:364:in `method_missing'
from /Users/sonat/.rvm/gems/ruby-1.8.7-p334/gems/activerecord-3.0.0/lib/active_record/attribute_methods.rb:46:in `method_missing'
from /Users/sonat/.rvm/gems/ruby-1.8.7-p334/gems/activerecord-3.0.0/lib/activ开发者_JAVA技巧e_record/attribute_methods.rb:44:in `send'

I can see that module that is responsible of the image_tag method is missing "something". I would appreciate if you guys tell me the general approach in finding what is missing and how I could solve the problem.


Looks like you're trying to do something odd like to use the helper method inside your model instead of inside your view. Helper methods are for views, not for models.

Added

Tim asked if I could provide some links to other resources, but really this is such basic stuff that I'm not aware of anything dealing with this. Mostly tutorials deal with unusual things like using helpers outside views.

So instead, here's some code:

app/helpers/contents_helper.rb

module ContentsHelper
  def foo
    content_tag :p, "Foo"
  end
end

app/views/contents/index.html.erb

<h1>Listing Contents</h1>

<%= foo %>

The HTML output when going to /contents URL

<h1>Listing Contents</h1>

<p>Foo</p>

And that's as hard as it is.


content_tag:

Does this work?

module ContentsHelper
 include ActionView::Helpers
end

image_tag:

Looks like you'll have to make your models know where the images are located by implementing a #path_to_image(image) method.

I've set up a Gist for us to mock out the features you need here:

https://gist.github.com/1015333

You should be able to download and run forNoNameNo.rb (which should run quietly without errors.)

That file has some examples of what's needed to make image_tag work.


further discussion:

Helpers are probably the weirdest part of Rails. They're declared at the controller level, but are only included at the view level, and rely on weird parts of the controller stack for their info; and yet, the info they are mainly concerned with presenting is the model info. So they're spread across all these weird concerns.

To deal with code-organizations issues arising from all of this, people have tended toward the Presenter pattern. (Also see rbates' version)

That said, if you'd prefer to stray off the beaten path in rails, it's normally really useful to read the source code.

If you're using textmate:

    mate `gem which actionpack`../action_view

in Terminal.app should get your there. If you're using another editor/operating system you might have to navigate to whereever gem which actionpack tells you to go.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜