我有两个以多对多关系建立的表格:事件和用户。当用户登录后,他们查看/事件页面(索引),我想要显示与它们关联的所有事件。不幸的是,出现以下错误: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