我有兩個以多對多關係建立的表格:事件和用戶。當用戶登錄後,他們查看/事件頁面(索引),我想要顯示與它們關聯的所有事件。不幸的是,出現以下錯誤:Rails:在多對多關係中找不到表格
Could not find table 'incidents_users'
看來Rails是尋找表「incidents_users」,當我真正創建表「users_incidents」。 'users_incidents'只保存user_id和incident_id。
我失去了一些東西明顯?我對rails比較陌生,所以問題可能很簡單,我忽略了。
這裏是incidents_controller.rb
# GET /incidents
# GET /incidents.xml
def index
@incidents = current_user.incidents
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @incidents }
end
end
這裏的相關部分是index.html.erb
<% for incident in @incidents %>
<tr>
<td><%=h incident.other_id %></td>
<td><%=h incident.title %></td>
<td><%= link_to 'Show', [@customer, incident] %></td>
<td><%= link_to 'Edit', edit_customer_incident_path(@customer, incident) %></td>
<td><%= link_to 'Destroy', [@customer, incident], :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
感謝有關節!請讓我知道,如果更多的信息會有所幫助。
那很簡單吧?謝謝! :) – Magicked 2010-04-22 13:11:59
哇,謝謝你的更新!我正在慢慢學習如何理解官方API文檔。我非常感謝你的幫助。 – Magicked 2010-04-22 13:23:52
沒問題:-)不要忘記Rails指南:http://guides.rubyonrails.org/ – 2010-04-22 13:27:33