A function doesn't work as expected, within a view


#1

Why does this view https://pastebin.com/raw/21tnpshE, display
puts “test” on the webpage? The view ends up with this html

But i’d have expected it to write “test”, to the console. My console only says test2

puts “test”


#2

Hi @barilo,

Ruby code needs to be surrounded by <% and %> for Rails to process it correctly, even inside of your function. Otherwise, it gets treated as HTML. Try writing it like this instead:

<% def aaa %>
  <% puts "test" %> <!-- this will go to the console like you want -->
  puts "Test 2" <!-- this will get treated as HTML and show up on the page -->
<% end %>

<% aaa %>

#3

Thanks. That works and explains that.


#4

This topic is solved.