Discussion Forums
- Topic List
- Most Recent Posts
- Sign In for more options
As Jason stated before, most of your helper methods will just have to return a string. However, there's another type of helper; the one that takes a block, commonly used to wrap a fragment of html inside some other, helper generated, html. For instance Rails' form_for method. This is how you create and use them:
def sidebar_box(title, &block)
content = capture(&block)
box = content_tag( :div, content_tag(:h2, title) + content, :class => "sidebar-box" )
concat(box) # In older versions of Rails also pass in the binding: concat(box, block.binding)
end
Will generate:
Register
register now!
Generally a helper will return HTML so that you can just do @@ in your view and it will be replaced with the HTML returned from @your_helper@. So in the helper method, you just do this like you would any string:
def your_helper(a)
%Q{[a href="/foo/bar"]#{a.titleize}[/a]} # those []s should be <>s but the forum won't let me
end
There are also all the view helpers available, so the above could also be done like this:
def your_helper(a)
content_tag :a, a.titleize, :href => '/foo/bar'
end
More information on "the view helpers can be found here":http://api.rubyonrails.org/ Just scroll down through the classes frame to @ActionView::Helpers::*@
Helpers are for views.Can any one tell me of how to use HTML Code on Helpers files with examples?
