在 Rails 中重新嵌套资源路由
ReNesting Resource Routes in Rails
所以我只是将我的一个对象作为我的用户对象的嵌套资源。现在我所有的链接都不起作用,我的索引也不会显示。我收到错误:
/views/photos/index.html.haml where line #8 raised:
No route matches {:action=>"show", :controller=>"photos", :id=>nil,
:user_id=>nil} missing required keys: [:id, :user_id]
这一行是它发现问题的地方:
= link_to photo.title, user_photo_path(@user, @photos)
这是我的照片对象控制器:
class PhotosController < ApplicationController
before_action :find_photo, only: [:show, :edit, :update, :destroy, :upvote, :downvote]
before_action :authenticate_user!, except: [:index, :show]
def index
@photos = Photo.all.order(:cached_weighted_score => :desc)
end
def show
@comments = Comment.where(photo_id: @photo)
@photo = Photo.find(params[:id])
end
我的路线是这样的:
Rails.application.routes.draw do
devise_for :users
root 'photos#index'
resources :users do
resources :photos do
member do
get "like", to: "photos#upvote"
get "unlike", to: "photos#downvote"
end
resources :comments
end
end
end
这是我的用户控制器:
class UsersController < ApplicationController
def show
@user = User.find_by(params[:id])
@photos= @user.photos.order(:cached_weighted_score => :desc)
end
end
最后生成错误代码的视图:
.container
.row
.col-lg-12
%h1.page-header Most Popular Photos
- @photos.each do |photo|
.thumbnail.col-lg-3.col-md-4.col-xs-6.thumb
%h2
= link_to photo.title, user_photo_path(@user, @photo)
= link_to (image_tag photo.image.url(:small)), photo
%p
= photo.get_likes.size
Likes
= link_to "Add New Photo", new_photo_path
感谢任何帮助。我最后的更改是将照片路由添加到用户路由下方。
第二个参数需要是循环中的照片实例而不是@photo,这可能是 nil:
= link_to photo.title, user_photo_path(@user, photo)
更新 1:您还需要在 PhotosController#show 中加载@user:
@user = User.find_by(params[:user_id])
所以我只是将我的一个对象作为我的用户对象的嵌套资源。现在我所有的链接都不起作用,我的索引也不会显示。我收到错误:
/views/photos/index.html.haml where line #8 raised:
No route matches {:action=>"show", :controller=>"photos", :id=>nil, :user_id=>nil} missing required keys: [:id, :user_id]
这一行是它发现问题的地方:
= link_to photo.title, user_photo_path(@user, @photos)
这是我的照片对象控制器:
class PhotosController < ApplicationController
before_action :find_photo, only: [:show, :edit, :update, :destroy, :upvote, :downvote]
before_action :authenticate_user!, except: [:index, :show]
def index
@photos = Photo.all.order(:cached_weighted_score => :desc)
end
def show
@comments = Comment.where(photo_id: @photo)
@photo = Photo.find(params[:id])
end
我的路线是这样的:
Rails.application.routes.draw do
devise_for :users
root 'photos#index'
resources :users do
resources :photos do
member do
get "like", to: "photos#upvote"
get "unlike", to: "photos#downvote"
end
resources :comments
end
end
end
这是我的用户控制器:
class UsersController < ApplicationController
def show
@user = User.find_by(params[:id])
@photos= @user.photos.order(:cached_weighted_score => :desc)
end
end
最后生成错误代码的视图:
.container
.row
.col-lg-12
%h1.page-header Most Popular Photos
- @photos.each do |photo|
.thumbnail.col-lg-3.col-md-4.col-xs-6.thumb
%h2
= link_to photo.title, user_photo_path(@user, @photo)
= link_to (image_tag photo.image.url(:small)), photo
%p
= photo.get_likes.size
Likes
= link_to "Add New Photo", new_photo_path
感谢任何帮助。我最后的更改是将照片路由添加到用户路由下方。
第二个参数需要是循环中的照片实例而不是@photo,这可能是 nil:
= link_to photo.title, user_photo_path(@user, photo)
更新 1:您还需要在 PhotosController#show 中加载@user:
@user = User.find_by(params[:user_id])