我試圖從特定筆記本中檢索筆記標題,只要用戶單擊它的鏈接並將它們顯示在另一列中。請對我溫柔,仍然是ruby/rails的新手。RoR從哈希中收集並在html中顯示它
我static_controller.rb
def index
@user = current_user
@notebooks = @user.notebooks
@notes_hash = @user.notes.group_by(&:notebook_id)
respond_to do |format|
format.html
format.js
end
end
我index.html.erb
<div id="notebooks-list">
<% @notebooks.each do |notebook| %>
<%= link_to notebook.description, "#", {:class => "notebook-link",
:remote => true, :data => notebook.id.to_json }%>
<% end %>
</div>
<div id="notes-list">
<script type="text/javascript">
$(".notebook-link").click(function() {
var id = this.getAttribute("data");
<%= notes = @notes_hash['id'] || [ ] %>
var notes = <%= notes.collect{ |a| a.id }.to_json %>
$("#notes-list").html(notes);
});
</script>
</div>
顯然,即時通訊做錯了什麼。
你爲什麼寫'裏面script'標籤的'div'您打算更換..它只會第一次工作,下次不會像腳本標籤被替換一樣工作。 – rubyprince 2012-02-04 07:52:26
謝謝,我現在要編輯它。 編輯:實際上,我試圖顯示var id,甚至與div內的JavaScript,由於某種原因它爲幾次點擊工作。 – 2012-02-04 07:53:57
好..什麼是你試圖顯示在分區..現在你正在發送筆記記錄ID數組作爲分區的內容.. – rubyprince 2012-02-04 08:04:22