Andrey Drozdov Andrey Drozdov - 2 months ago 8x
jQuery Question

Rails, add ruby parameter to javascript

I have a div, and I want it to be a link to other page. Usually we do it like this

link_to link

<% for post in @posts %>
<%= link_to "Post", post %> # -> or <%= link_to(post) %>
<% end %>

But i need to whole div to be a link with js. So, i did:

<% for post in @posts %>
<div class="post-on-main-page-<%= div_count %>" >
$(".post-on-main-page-<%= div_count %>").click(function(){
window.location.href = "<%= link_to(post) %>";
<% end %>

But i doesn't work.

I need to
window.location.href = "<%= link_to(post) %>";
where post is a parameter, to give me a link to that post.

So how I can make this to work?


link_to will generate a full <a> element

You should use post_path(post) or post_url(post) instead if you just want the path or url