I am having issues getting JQuery to be parsed by ruby.
For instance, the following line of code is supposed to add the @user's username after the div with id 'title.'
$('#title').after('<%= escape_javascript(@user.first.username') %>);
However, the output that I receive is a string that hasn't been parsed at, complete with <
I haven't found anything specifically posted on this on Stack Overflow and I've followed several tutorials unsuccessfully.
Thanks in adva开发者_如何学编程nce for your help!
erb is parsed before the page loads, so this is inserting the erb code during javascript execution, which doesn't get parsed at all. A better way to do this one would be to insert JS to set a variable, and then use that variable:
<script>
var myObj = "<%= escape_javascript(@user.first.username) %>";
$('#title').after(myObj);
</script>
I would argue you should not take this approach... Placing script tags in your erb is a smell IMO. You would be better off creating a hidden input field with the username and an id of 'user_name' and then using unobtrusive js in your app.js to get the field value and append it.
$(document).ready(function(){
$('#title').after($('input#user_name').val());
});
精彩评论