MrBiggs
MrBiggs

Reputation: 163

Pusher - adding ruby to javascript string?

I'm trying to implement Pusher to my rails app and have it working in principal however I am struggling to add ruby code to the string of data being pushed to the client to create a url using link_to. I'm struggling to understand the problem as the javascript console is saying:

Uncaught SyntaxError: Unexpected token %

<script src="http://js.pusher.com/2.2/pusher.min.js"></script>
<script type="text/javascript">
var pusher = new Pusher('<%= Pusher.key %>'); // uses your API KEY
var channel = pusher.subscribe('worklink');
channel.bind('update', function(data) {

$("#comments").prepend(

"<div class='post_box'><div class='post'><p class='username'><b><%= link_to " + data.userfirstname + " " + data.userlastname + ", user_path(" + data.userid + ") %></b></p><p class='post_content'>" + data.postcontent + "</p></div></div>");

});
</script>

Any help would be much appreciated.

Upvotes: 1

Views: 98

Answers (1)

gdpelican
gdpelican

Reputation: 568

Surya is right on; you'll need to construct the link in ruby, then pass it out as javascript.

Assuming you have a @user variable populated in your view (perhaps you can use current_user?):

var prependLink = "<%= link_to "#{@user.first_name} #{@user.last_name}", user_path(@user) %>"
$('#comments').prepend(prependLink);

Upvotes: 1

Related Questions