Ruby on rails Rails 3嵌套资源视图(索引)
因此,我最终使用嵌套资源使我的URL正常工作,但仍然存在一个小问题。我的mysite.com/profile/1/photos/new页面和表单工作正常,但mysite.com/profile/1/photos页面不工作。我不知道这是为什么 我的嵌套路由看起来是这样的Ruby on rails Rails 3嵌套资源视图(索引),ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,因此,我最终使用嵌套资源使我的URL正常工作,但仍然存在一个小问题。我的mysite.com/profile/1/photos/new页面和表单工作正常,但mysite.com/profile/1/photos页面不工作。我不知道这是为什么 我的嵌套路由看起来是这样的 resources :profiles do resources :photos end 索引页将不工作,它给我以下错误 undefined method `user_id' 这是我的photos_controller.rb
resources :profiles do
resources :photos
end
索引页将不工作,它给我以下错误
undefined method `user_id'
这是我的photos_controller.rb文件中的内容
def index
@profile = Profile.find(params[:profile_id])
@photo = Photo.find_by_id(params[:all])
end
def show
@profile = Profile.find(params[:profile_id])
@photo = Photo.find(params[:id])
end
这是索引表。这个表格不起作用
<% title "Photos" %>
<table>
<tr>
<th>User</th>
<th>Title</th>
<th>Description</th>
</tr>
<% for photo in ([@photos, @profile]) %>
<tr>
<td><%= photo.user_id %></td>
<td><%= photo.title %></td>
<td><%= photo.description %></td>
<td><%= link_to "Show", profile_photo_path %></td>
<td><%= link_to "Edit", edit_profile_photo_path%></td>
<td><%= link_to "Destroy", profile_photo, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
使用者
标题
描述
“你确定吗?”,:method=>:delete%>
mysite.com/photos
应该是mysite.com/profiles/:profile\u id/photos
和
photos/edit
应该是profiles/:profile\u id/photo/:id/edit
链接帮助程序看起来像
link_to "Photos", profile_photos_path(@profile)
link_to "Edit photo", profile_edit_photo_path(@profile,@photo)
查看rails指南关于