我有一個div,我希望它成爲其他頁面的鏈接。通常我們會這樣做link_to link
。Rails,爲javascript添加紅寶石參數
<% for post in @posts %>
<%= link_to "Post", post %> # -> or <%= link_to(post) %>
<% end %>
但我需要整個div與js的鏈接。所以,我做了:
<% for post in @posts %>
<div class="post-on-main-page-<%= div_count %>" >
<script>
$(".post-on-main-page-<%= div_count %>").click(function(){
window.location.href = "<%= link_to(post) %>";
});
</script>
</div>
<% end %>
但我沒有工作。
我需要window.location.href = "<%= link_to(post) %>";
其中post是一個參數,給我一個該帖子的鏈接。
那麼我如何才能使這個工作?
爲什麼不把數據屬性添加到'div'並使用該值來重定向? –
試試這個'window.location.href =「#{link_to(post)}」; –
@BrenoPerucchi沒有那麼壞 – naomik