Hey guys
I’m new to rails. I made this small test code for learning helper in rails:
apps/helpers/home_helper.rb
module HomeHelper
def show(var)
yield var
end
end
apps/views/home/index.html.rb
<%= show('hello world')%>
When I navigate to the url localhost:3000/home/index I got nothing in the html source
What did I do wrong?
There are few things to note here:
Firstly you’re using
yieldwhich will pass control to the block given to the method. However, you then call the method without a block:If you would have had a block it would have looked something like this:
This would have output ‘hello world’ as you expected.
Most like you meant:
This returns the value you’re passing in and will output it to the response stream.
While block helpers can often to be useful for drying up your code most of the time you want a partial with a layout.