exsnake
exsnake

Reputation: 1823

Use JQuery for change a image in Ruby on Rails

I'm having problems to change an image using JQuery.

The fragment from my hmtl.erb:

<%= image_tag('empty-screen.png', style:'width: 100%', class:'img-responsive center-block', id:'screen-d') %>

Fragment from my javascript:

$("#collapseOne").on('show.bs.collapse',function () {
        $('#screen-d').attr("src","#{asset_path('empty-screen.png')}");
    });

The real problem is that I don't know very well how Rails uses the asset pipeline. I believed that using asset_path would work, but with that code I was just changing the src to #{asset_path('empty-screen.png')}. So I don't really know what to do.

Upvotes: 0

Views: 395

Answers (1)

Chen Kinnrot
Chen Kinnrot

Reputation: 21015

Try

$("#collapseOne").on('show.bs.collapse',function () {
    $('#screen-d').attr("src","<%= asset_path('empty-screen.png') %>");
});

And make sure to add a .erb at the end of your .js file

Upvotes: 1

Related Questions