簡體   English   中英

Rails 3-正確的方式給belongs_to模型的ID

[英]Rails 3 - correct way to give id for belongs_to model

我有兩個模型: UserTeacher

class User < ActiveRecord::Base
  attr_accessor   :password                                                               
  attr_accessible :user_login,                                                           
                  :user_role,
                  :password,
                  :teacher_attributes

  has_one :teacher
  accepts_nested_attributes_for :teacher                                                  
end

# Table name: teachers
#
#  id                  :integer         not null, primary key
#  user_id             :integer
#  teacher_last_name   :string(255)
#  teacher_first_name  :string(255)
#  teacher_middle_name :string(255)
#  teacher_birthday    :date
#  teacher_sex         :string(255)
#  teacher_category    :string(255)
#  created_at          :datetime
#  updated_at          :datetime

class Teacher < ActiveRecord::Base
  attr_accessible :teacher_last_name,
                  :teacher_first_name,
                  :teacher_middle_name,
                  :teacher_birthday,
                  :teacher_sex,
                  :teacher_category

  belongs_to :user
end

我的控制器中也有一個方法,該方法應先創建用戶,然后創建新老師。 但是無法正常工作,因為我沒有在此處傳遞id

我的控制器

class AdminsController < ApplicationController
  def create_teacher
    user_errors, teacher_errors, redirect_path = nil, nil, nil 

    params[:user][:user_role] = "teacher"
    user = User.new(params[:user])                           #Problem is here

    if user.save
      redirect_path = admins_users_of_system_path
      flash[:success] = "Teacher created!"
    else
      redirect_path = admins_new_teacher_path
      user_errors = user.errors.full_messages.to_sentence                                     
      teacher_errors = user.teacher.errors.full_messages.to_sentence if user.teacher
    end

    errors_arr = [user_errors, teacher_errors].compact
    flash[:error] = errors_arr if errors_arr.present? 
    redirect_to redirect_path
  end
end

我在控制器中有這樣的代碼,因為我想抓住用戶和老師的所有錯誤。

錯誤

["Teacher user can't be blank", "User can't be blank"]

utf8: ✓
authenticity_token: Hsw9BGOMb37cku6R2aAZsXkYaU5DFZqlUML4yZjqxI0=
user: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  teacher_attributes: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
    teacher_last_name: Popov
    teacher_first_name: Igor
    teacher_middle_name: Yurevich
    teacher_sex: m
    teacher_birthday: 28.08.1991
    teacher_category: Something 1
  user_login: schoolh_MXWTQ
  password: eviRDyalXnHlK6q
  user_role: teacher
commit: Создать
controller: admins
action: create_teacher

如何正確同時創建教師和用戶? 我應該如何傳遞一個ID?

一種不同的方法是在第一輪中簡單地創建用戶,包括老師的角色。 然后,管理員用個人資料詳細信息填寫第二個表單,如果可以,則填寫“在個人資料中填寫”(教師模型仍將包含此信息。)

無論哪種方式,您都可能希望使用委托方法來訪問方法http://apidock.com/rails/v3.1.0/Module/delegate

另外,無論您如何做,您都可以考慮將attr_accessible保留,直到該東西正常工作為止。

但是,如果您找到一次同時創建它們的方法,我將是第一個復制您的方法!

我認為概率是參數,你給錯了。 正確的方法應該是: params [:user] ['user_role'] =“ teacher”

編輯:正如我在評論中所說:

嘗試:

user = User.create(params[:user])

而不是User.new

暫無
暫無

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

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