簡體   English   中英

Rails為nil:NilClass使用的未定義方法`email'

[英]Rails undefined method `email' for nil:NilClass

我在Rails應用程序中收到以下錯誤。 你能幫助我嗎?

連接兩個表時出現錯誤。 用戶想要使用表格。 我正在嘗試從單個表中管理經理ID和用戶ID字段。

紅寶石2.5.1p57(2018-03-29修訂版63029)[x86_64-darwin17]

導軌5.2.2

數據庫設計

錯誤信息

數據庫設計和表schema.rb:

ActiveRecord::Schema.define(version: 2019_02_24_160401) do

  create_table "projects", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
    t.string "name"
    t.text "description"
    t.string "company"
    t.bigint "manager_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["manager_id"], name: "index_projects_on_manager_id"
  end

  create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "username", default: "", null: false
    t.string "fullname", default: ""
    t.bigint "manager"
    t.string "company", default: ""
    t.string "department", default: ""
    t.boolean "isadmin", default: 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.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

  add_foreign_key "projects", "users", column: "manager_id"
end

projects_controller.rb

class ProjectsController < ApplicationController
  before_action :set_project, only: [:show, :edit, :update, :destroy]
  before_action :find_users, only: [:index, :show, :new, :edit]
  # GET /projects
  # GET /projects.json
  def index
    @projects = Project.all
  end

  # GET /projects/1
  # GET /projects/1.json
  def show
  end

  # GET /projects/new
  def new
    @project = Project.new
  end

  # GET /projects/1/edit
  def edit
  end

  # POST /projects
  # POST /projects.json
  def create
    @project = Project.new(project_params)

    respond_to do |format|
      if @project.save
        format.html { redirect_to @project, notice: 'Project was successfully created.' }
        format.json { render :show, status: :created, location: @project }
      else
        format.html { render :new }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /projects/1
  # PATCH/PUT /projects/1.json
  def update
    respond_to do |format|
      if @project.update(project_params)
        format.html { redirect_to @project, notice: 'Project was successfully updated.' }
        format.json { render :show, status: :ok, location: @project }
      else
        format.html { render :edit }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /projects/1
  # DELETE /projects/1.json
  def destroy
    @project.destroy
    respond_to do |format|
      format.html { redirect_to projects_url, notice: 'Project was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_project
      @project = Project.find(params[:id])

    end

    def find_users
      @users = User.all.order('created_at desc')
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def project_params
      params.require(:project).permit(:name, :description, :user_id)
    end
end

模型文件夾project.rb和user.rb中的模型文件

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  has_many :projects
end

class Project < ApplicationRecord
  belongs_to :user
end

調用我的代碼電子郵件列index.html.erb

<p id="notice"><%= notice %></p>

<h1>Projects</h1>

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Description</th>
      <th>Company</th>
      <th>Manager</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @projects.each do |project| %>
      <tr>
        <td><%= project.name %></td>
        <td><%= project.description %></td>
        <td><%= project.company %></td>
        <td><%= project.user.email %></td>
        <td><%= link_to 'Show', project %></td>
        <td><%= link_to 'Edit', edit_project_path(project) %></td>
        <td><%= link_to 'Destroy', project, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Project', new_project_path %>

看來數據在架構中的草稿不夠好。 UserManager是同一個人嗎? 如果不是,則在Projects表中應該有一個user_id引用,否則,您必須添加將manager_id綁定到Users適當關聯,然后才能擁有project.manager.email

您的Project有一個manager_id ,但沒有名為managers表,而是users 因此,在項目與用戶之間的關系中,它需要使用正確的密鑰。

class User < ApplicationRecord
  has_many :projects, foreign_key: :manager_id
end

class Project < ApplicationRecord
  belongs_to :user, foreign_key: :manager_id
end

如果用戶對於Project是可選的,只需調用project.user&.email

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM