找不到 'id'= 的用户
Couldn't find User with 'id'=
我知道这是最常见的问题,但我真的想在这里寻求帮助,因为我不明白我在这里做错了什么。
我生成了一个设计用户并打算为用户创建一个图库。以下是我试过的代码
型号 - Gallery.rb
class Gallery < ActiveRecord::Base
belongs_to :user
end
控制器 - galleries_controller.rb
class GalleriesController < ApplicationController
before_action :set_gallery, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user!
respond_to :html
def index
@user = User.find(params[:user_id])
@galleries = Gallery.all
respond_with(@galleries)
end
def show
respond_with(@gallery)
end
def new
@user = User.find(params[:user_id])
@gallery = Gallery.new
respond_with(@gallery)
end
def edit
end
def create
@gallery = Gallery.new(gallery_params)
flash[:notice] = 'Gallery was successfully created.' if @gallery.save
respond_with(@gallery)
end
def update
flash[:notice] = 'Gallery was successfully updated.' if @gallery.update(gallery_params)
respond_with(@gallery)
end
def destroy
@gallery.destroy
respond_with(@gallery)
end
private
def set_gallery
@gallery = Gallery.find(params[:id])
end
def gallery_params
params[:gallery, :user_id]
end
end
以下是与更好的错误一起显示的错误 -
日志
Started GET "/galleries/1" for 127.0.0.1 at 2015-05-03 15:24:10 +0530
Processing by GalleriesController#show as HTML
Parameters: {"id"=>"1"}
Gallery Load (2.1ms) SELECT "galleries".* FROM "galleries" WHERE "galleries"."id" = ? LIMIT 1 [["id", 1]]
Completed 404 Not Found in 63ms
ActiveRecord::RecordNotFound - Couldn't find Gallery with 'id'=1:
activerecord (4.2.0) lib/active_record/core.rb:154:in `find'
routes.rb
Rails.application.routes.draw do
root to: 'visitors#index'
devise_for :users
resources :users do
resources :galleries
resources :photos
end
end
由于我对 ROR 还是个新手,我们将不胜感激任何帮助。提前致谢。
从 URL 我假设你没有在参数中传递 user_id。
在你的路由文件库中应该是嵌套路由。如果你不想 user_id.
resources :users do
resources :galleries do
end
end
或者您可以使用
@user= current_user
我知道这是最常见的问题,但我真的想在这里寻求帮助,因为我不明白我在这里做错了什么。
我生成了一个设计用户并打算为用户创建一个图库。以下是我试过的代码
型号 - Gallery.rb
class Gallery < ActiveRecord::Base
belongs_to :user
end
控制器 - galleries_controller.rb
class GalleriesController < ApplicationController
before_action :set_gallery, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user!
respond_to :html
def index
@user = User.find(params[:user_id])
@galleries = Gallery.all
respond_with(@galleries)
end
def show
respond_with(@gallery)
end
def new
@user = User.find(params[:user_id])
@gallery = Gallery.new
respond_with(@gallery)
end
def edit
end
def create
@gallery = Gallery.new(gallery_params)
flash[:notice] = 'Gallery was successfully created.' if @gallery.save
respond_with(@gallery)
end
def update
flash[:notice] = 'Gallery was successfully updated.' if @gallery.update(gallery_params)
respond_with(@gallery)
end
def destroy
@gallery.destroy
respond_with(@gallery)
end
private
def set_gallery
@gallery = Gallery.find(params[:id])
end
def gallery_params
params[:gallery, :user_id]
end
end
以下是与更好的错误一起显示的错误 -
日志
Started GET "/galleries/1" for 127.0.0.1 at 2015-05-03 15:24:10 +0530
Processing by GalleriesController#show as HTML
Parameters: {"id"=>"1"}
Gallery Load (2.1ms) SELECT "galleries".* FROM "galleries" WHERE "galleries"."id" = ? LIMIT 1 [["id", 1]]
Completed 404 Not Found in 63ms
ActiveRecord::RecordNotFound - Couldn't find Gallery with 'id'=1:
activerecord (4.2.0) lib/active_record/core.rb:154:in `find'
routes.rb
Rails.application.routes.draw do
root to: 'visitors#index'
devise_for :users
resources :users do
resources :galleries
resources :photos
end
end
由于我对 ROR 还是个新手,我们将不胜感激任何帮助。提前致谢。
从 URL 我假设你没有在参数中传递 user_id。
在你的路由文件库中应该是嵌套路由。如果你不想 user_id.
resources :users do
resources :galleries do
end
end
或者您可以使用
@user= current_user