如果我访问http://localhost:3000/pics/9或任何帖子,我会收到此错误
Showing c:/Sites/insta/app/views/pics/show.html.haml where line #5 raised:
undefined method `email' for nil:NilClass
Rails.root: c:/Sites/insta
Application Trace | Framework Trace | Full Trace
app/views/pics/show.html.haml:5:in `_app_views_pics_show_html_haml__310592347_80083632'
show.html.haml
%h1= @pic.title
%p= @pic.description
%p
Pic by
= @pic.user.email
%br
= link_to "Back", root_path
= link_to "Edit", edit_pic_path
= link_to "Delete", pic_path, method: :delete, data: { confirm: "Are you sure?" }
shema.rb
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160825161502) do
create_table "pics", force: :cascade do |t|
t.string "title"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
end
add_index "pics", ["user_id"], name: "index_pics_on_user_id"
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
我搜索解决方案的问题在哪里,但我找不到任何内容
答案 0 :(得分:0)
问题是您没有user
与导致此错误的某些Pic
相关联:
未定义的方法`电子邮件'为零:NilClass
您可以通过更改
来解决此问题= @pic.user.email
在show.html.haml
行5
到
= @pic.user.try(:email)