DaudiHell
DaudiHell

Reputation: 808

random header images ruby on rails

I´m using the following code to randomize images in a header on a category page but I´m really not understanding why this method isn´t working.

it gives me this error Invalid CSS after "...ackground: url(": expected ")", was "<%= randomized_..."

in this line #necklace_header { background: url(<%= randomized_header_image %>) no-repeat center center fixed; width: 100%;

in my views/categories/show.html.erb I have this code

<header id="necklace_header">
<h1>
  <%= @category.name %>
</h1>
</header>



<%= render "categories/table", products: @products %>



<% if current_user && current_user.admin? %>
    <%= link_to 'Edit', edit_category_path(@category) %> |
<% end %>

<%= link_to 'Back', root_path %>

In my categories.scss I have this piece of code

#necklace_header {
    background: url(<%= randomized_header_image %>) no-repeat center center fixed;
    width: 100%;


    background-size: cover;  

    height: 360px;
    margin-bottom: 20px; 

}

And in the application_helper.rb I got this code

module ApplicationHelper

    def randomized_header_image
        images = ["assets/foo.jpg", "assets/random.jpg", "assets/super_random"]
        images[rand(images.size)]
    end

end

since I´m pretty much a newbie to rails and web coding in general I have not been able to figure this out. I would be great if someone could clarify this for me.

Upvotes: 0

Views: 91

Answers (1)

Arup Rakshit
Arup Rakshit

Reputation: 118289

As OP confirmed my suggestion worked. I am going to turn it back to an answer for future readers.

You have .scss file, so ERB code will not work. But try to write it like asset-url('randomized_header_image') and it should work. asset-url is SASS helper.

Upvotes: 2

Related Questions