1

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.

1
  • You can't pass your JavaScript variables to Ruby code. You need to do vise-a-versa. Commented Oct 4, 2014 at 12:19

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.