-3

I was trying to add javascript variable in link_to. but name appear as "+item_id+" not as a variable. my code is below.

  var item_id = item[0]

  $('#item').append('<%= link_to '+ item_id +' , prd_item_path('+ item_id+') %>')

2 Answers2

1

You're mixing server side with client side code. Keeping in mind that the link_to helper is rendered as a "normal" anchor tag, and that you can't mix the ERB code passing JS values, then you can use that anchor, and concatenate the JS values.

Try with:

$('#item').append('<a href="item/' + item_id + '">' + item_id + '</a>')

Being item/ the URI of prd_item_path.

Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
0

You can't create ERB elements at the client side. ERB is rendered at the server side.

Create the link at the client using JS, as suggested here

Guy Yogev
  • 861
  • 5
  • 14