控制器中的实例变量定义问题:Rails 5

时间:2017-08-24 02:08:07

标签: ruby-on-rails instance-variables

我有一个用户表,其中两个角色使用枚举和单表继承定义,其中两个匹配的用户子类用于Staff和Clinician。因为我需要大量关于临床医生的信息,我不需要关于Staff,我已经创建了一个clinician_profiles表,并且在用户模型中使用了after_create:create_clinician_profile来为临床医生简介创建存根。

我正在尝试为临床医生构建一个页面来完成他们的个人资料,并且无法在控制器中定义实例变量,因此它会将相应的临床医师的个人资料拉入视图以呈现表单。我收到以下错误。请注意,我登录的用户ID是104。

ActiveRecord::RecordNotFound in ClinicianProfilesController#edit
Couldn't find ClinicianProfile with 'id'=104

我觉得我很亲密,但不知道我做错了什么。对于一个新手来说,任何帮助都会非常感激!

我使用标题中的此链接登陆页面:

<li><%= link_to "Manage My Profile", edit_clinician_profile_path(current_user) %></li>

以下是我的用户模型的相关部分:

class User < ApplicationRecord
  self.inheritance_column = :role
  enum role: { Staff: 0, Clinician: 1}
  belongs_to :university
  has_many :referral_requests

class Staff < User
  validates :university_id, presence: true
end

class Clinician < User
  has_many :lists
  has_many :universities, through: :lists
  has_one :clinician_profile
  after_create :create_clinician_profile
end

这是我的ClinicianProfile模型:

class ClinicianProfile < ApplicationRecord
    has_many :clinician_profile_languages
    has_many :languages, through: :clinician_profile_languages
    has_many :clinician_profile_races
    has_many :races, through: :clinician_profile_races
    belongs_to :clinician
end

clinician_profiles架构

create_table "clinician_profiles", force: :cascade do |t|
    t.datetime "created_at",          null: false
    t.datetime "updated_at",          null: false
    t.string   "firstname"
    t.string   "lastname"
    t.string   "address1"
    t.string   "address2"
    t.string   "city"
    t.string   "state"
    t.string   "zip"
    t.boolean  "accepting_patients"
    t.integer  "rate"
    t.string   "license_number"
    t.string   "license_state"
    t.string   "school"
    t.integer  "year_graduated"
    t.string   "accepts_insurance"
    t.boolean  "sliding_scale"
    t.text     "bio"
    t.boolean  "verified"
    t.integer  "years_licensed"
    t.integer  "years_of_experience"
    t.integer  "clinician_id"
  end

ClinicianProfilesController

class ClinicianProfilesController < ApplicationController
def edit
    @clinician_profile = ClinicianProfile.find(params[:id])
end

def index
end

def show
end

def create
end

end

以下是我的观点的开头(/ views / clinician_profiles中的edit.html.erb)

<%= form_for(@clinician_profile) do |f| %>
<%= render 'shared/error_messages', object: f.object %>

1 个答案:

答案 0 :(得分:-1)

def edit
    @clinician_profile = ClinicianProfile.find(params[:id])
    redirect_to :index and return unless @clinician_profile
end