0

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.

1 Answer 1

1

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

<%= panel_color %>

def panel_color
  ["#E5E0AE","#A4D349","#F1427B","#F09137","#792060"].sample
end
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Dave. I feel like a moron.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.