2016-08-26 61 views
0

如果我查看在http://localhost:3000/pics/9或任何后我得到这个错误NoMethodError太平洋岛屿#显示

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

@ pic.user是无,无论图片中的记录是不是属于任何用户或它有user_id ni这就是你得到这个错误的原因。 – Navin

回答

0

问题是你没有与一些Pic的,这是造成此错误关联user:在show.html.haml线5改变

= @pic.user.email 

undefined method `email' for nil:NilClass

就可以解决这个问题

= @pic.user.try(:email)