ruby - /gigs/:id/edit 处的 NoMethodError
问题描述
我正在尝试在我的应用程序上“编辑演出”,但它给了我演出没有 user_id 的错误。创建一个新的演出是没有问题的。我觉得这只是我没有正确查看的糟糕语法,但我不知道。我什么都试过了。
NoMethodError at /gigs/:id/edit
undefined method `user_id' for nil:NilClass
file: gigs_controller.rb location: block in <class:GigController> line: 43
下面是我的 App Controller、Gig Controller、架构和日志。谢谢!
应用程序控制器
require './config/environment'
class ApplicationController < Sinatra::Base
configure do
set :public_folder, 'public'
set :views, 'app/views'
enable :sessions
set :session_secret, "secret"
end
get '/' do
erb :welcome
end
get '/logout' do
session.clear
redirect to '/'
end
#helpers
helpers do
def logged_in?
!!current_user
end
def current_user
User.find_by_id(session[:user_id])
end
def is_authorized?(id)
current_user.id == id.to_i
end
end
end
演出控制器
class GigController < ApplicationController
get '/gigs' do
if logged_in?
@gigs = Gig.all
erb :'/gigs/index'
else
redirect to '/signup'
end
end
get '/gigs/new' do
if logged_in?
@gigs = Gig.all
erb :'gigs/new'
else
redirect to '/login'
end
end
post '/gigs' do
if logged_in?
@gig = current_user.gigs.create(bands: params[:bands], location: params[:location], date: params[:date], time: params[:time])
"Event has been added."
redirect "/gigs"
else
"When creating a new Event, please provide Artists, a Location, a date and a time."
redirect '/gigs'
end
end
get '/gigs/:id' do
if logged_in?
@gig = Gig.find(params[:id])
erb :"gigs/show"
else
redirect '/login'
end
end
get '/gigs/:id/edit' do
if logged_in?
@gig = Gig.find(params[:id])
if current_user.id == @gig.user_id
erb :"gigs/edit"
else
"You are not authorized to edit this Event."
redirect to '/login'
end
end
end
end
架构-
ActiveRecord::Schema.define(version: 20200209173236) do
create_table "genres", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "gigs", force: :cascade do |t|
t.string "bands"
t.string "location"
t.date "date"
t.string "time"
t.integer "user_id"
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.string "password_digest"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
错误-
D, [2020-02-10T13:53:35.237402 #11377] DEBUG -- : ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
D, [2020-02-10T13:53:35.257906 #11377] DEBUG -- : User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 4]]
D, [2020-02-10T13:53:35.262209 #11377] DEBUG -- : Gig Load (0.1ms) SELECT "gigs".* FROM "gigs" WHERE "gigs"."id" = ? LIMIT 1 [["id", nil]]
2020-02-10 13:53:35 - ActiveRecord::RecordNotFound - Couldn't find Gig with 'id'=:
/Users/melc/.rvm/gems/ruby-2.6.1/gems/activerecord-4.2.11.1/lib/active_record/core.rb:155:in `find'
/Users/melc/nyc-shows/app/controllers/gigs_controller.rb:42:in `block in <class:GigController>'
解决方案
推荐阅读
- phpstorm - LiveEdit 在外部更新时看不到 CSS 文件的更改
- shell - 修改 grep 结果以打印匹配字符串中的特定单词
- python - Jupyter Notebook:文件“.ipynb_checkpoints”不存在 - Python
- forms - Delphi 2007 带两个显示器的系统的模态表单位置
- attributes - 平面表中缺少自定义属性
- php - PHP上传/下载文件的大小限制
- javascript - 参数数量不足或找不到条目
- php - 如何在视图codeigniter中自动显示自动生成ID
- javascript - 在 Angular 6 运行时动态导入 JS 库
- c - 自旋锁初始化函数