Rails 3 - 为belongs_to模型提供id的正确方法

时间:2012-03-15 13:09:23

标签: ruby-on-rails ruby

我有两种模式:用户教师

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

如何正确创建教师和用户?我该怎么把它传给你?

3 个答案:

答案 0 :(得分:1)

另一种方法是在第一轮创建用户,包括教师的角色。然后,管理员填写第二个表单,其中包含个人资料详细信息,如果您愿意,请填写“填写您的个​​人资料”(教师模型仍然包含此内容。)

无论哪种方式,您可能都希望使用delegates_to方法来访问方法http://apidock.com/rails/v3.1.0/Module/delegate

此外,无论你如何做,你可能只考虑将attr_accessible保留,直到其他方式正常工作。

但如果你找到一种方法同时创建它们,我会是第一个复制你的人!

答案 1 :(得分:0)

我认为问题是假的,你说错了。 正确的方法应该是: params [:user] ['user_role'] =“老师”

答案 2 :(得分:0)

编辑: 正如我在评论中所说:

尝试:

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

而不是User.new