port5432
port5432

Reputation: 6371

Rails: passing variable to jQuery

This should be simple but I am stuck. I am trying to pass a value from erb to jQuery in a Rails app. There are two popups. One on page load and another on a button click. Both return undefined.

Also, is there a simpler form of the content_tag I can use for passing variables?

show.html.erb

<%= content_tag "div", id: "batch_status", data: {batch_status: @batch.status} do %><% end %>

rendered page html source

<div data-batch-status="completed" id="batch_status"></div>

batches.js.coffee

jQuery ->
  alert $('#batch_status').data('batch_status')

  $("#apply_btn").on "click", ->
    alert $('#batch_status').data('batch_status')

Upvotes: 0

Views: 288

Answers (1)

alek
alek

Reputation: 331

Try this:

<%= content_tag "div", id: "batch_status", 'data-batch_status' => @batch.status do %><% end %>

Upvotes: 1

Related Questions