Nic
Nic

Reputation: 13733

Helper function not returning string

So I'd like to generate a random background-color based on an array:

def panel_color
 a = ["#E5E0AE","#A4D349","#F1427B","#F09137","#792060"]
 return a.sample
end

Simple enough. This is to be used in my disc#index.erb view, so I call it there:

...
<div class="panel" style="background-color: <% panel_color %>;">
...

Since this is a helper method for the view, I placed the function in helpers/disc_helper.rb

module DiscHelper
 def panel_color
  a = ["#E5E0AE","#A4D349","#F1427B","#F09137","#792060"]
  return a.sample
 end
end

Which, to my surprise, returns nothing to the view, but does not error, either. I'm thinking I missed something very obvious here, but I'm not quite sure what. Latest rails here.

Upvotes: 0

Views: 632

Answers (1)

Dave Newton
Dave Newton

Reputation: 160211

You're just executing it, not displaying it. Use <%= ... %> instead:

<%= panel_color %>

def panel_color
  ["#E5E0AE","#A4D349","#F1427B","#F09137","#792060"].sample
end

Upvotes: 1

Related Questions