2015-10-13 27 views
0

在我的應用程序中,我有一個筆記的概念...在頁面上,我用編輯按鈕列出了所有筆記。這個編輯按鈕打開一個編輯音符的模式。除了一個大問題,這一切都很好。如果我有5個音符,當我點擊第一個編輯按鈕時,它會按預期編輯第一個音符......但是如果我點擊第二個編輯按鈕,它也會編輯第二個音符的第一個音符INSTEAD。基本上不管我點擊哪個編輯按鈕,我總是編輯第一個音符。如何在本地人頁面上多次渲染相同的部分

用戶控制器

def show 
    @user = User.find(params[:id]) 
    @notes = @user.notes.order('created_at DESC') 
end 

索引頁

<% @notes.each do |note| %> 
    <td><%= note.body %></td> 
    ........ 
    <td> 
    <%= button_tag text, class: "btn btn-primary", data: {toggle: "modal", target: "#edit_note"} %> 
    <%= render partial: "edit_note_modal", locals: { note: note } %> 
    </td> 
<% end %> 

注模態

<div class="modal fade" id="edit_note" tabindex="-1" role="dialog" aria-labelledby="edit_note" aria-hidden="true"> 
    <div class="modal-dialog"> 
    <div class="modal-content"> 
     <div class="modal-header"> 
     <h4 class="modal-title">Edit Note</h4> 
     </div> 
     <div class="modal-body"> 
     <%= form_for note do |f| %> 
      <div class="form-group"> 
      <label for="keyword">Keyword</label> 
      <%= select_tag :keyword, options_for_select(<SOME FUNCTION>, selected: note.keyword), class:"form-control" %> 
      </div> 
      <div class="form-group"> 
      <label for="body">Body</label> 
      <%= text_area_tag :body, note.body , class:'form-control' %> 
      </div> 
      <div class="modal-footer"> 
      <%= submit_tag "Update Note", class: "btn btn-primary" %> 
      </div> 
     <% end %> 
     </div> 
    </div> 
    </div> 
</div> 
+2

可以顯示填充@notes的控制器,代碼對我來說看起來很好。 – matanco

+0

@matanco完成。謝謝 – Zack

+0

這段代碼看起來不錯,但是你的控制器的編輯/更新操作是什麼? – Elvn

回答

1

這是因爲ÿ ou使用相同的ID和ID應該是uniq,因此當它在索引頁面中搜索到ID時,它會打開第一個註釋,ID爲edit_note它會打開它,您可以做的是爲每個視圖使用不同的Id,如下所示:

索引頁:

<%= button_tag text, class: "btn btn-primary", data: {toggle: "modal", target: "#edit_note_#{note.id}"} %> 

注模態:

<div class="modal fade" id="edit_note_#{note.id}" tabindex="-1" role="dialog" aria-labelledby="edit_note" aria-hidden="true"> 

注意ID現在是如何edit_note_#{note.id}和編輯按鈕嘗試打開模型相同的目標edit_note_#{note.id}