0

我有以下控制器: 1-students_controller.rb

class StudentsController < ApplicationController
  def index
    @students = Student.all
  end

  def show
    @student = Student.find(params[:id])
  end

  def new
    @student = Student.new
  end

  def create
    @student = Student.new(params[:student])
    if @student.save
      flash[:notice] = ' Student Record Saved Successfully. Please fill the Parent Details.'
      redirect_to new_student_guardian_path(@student.id)
    else
      flash[:error] = 'An error occurred please try again!'
      render 'new'
    end
  end

  def edit
  end
end

2- Guardians_controller.rb

class GuardiansController < ApplicationController
  before_filter :set_student, only: [:new, :create]
  def index
    @guardian = Guardian.all
  end

  def show
    @guardian = Guardian.find(params[:id])
  end

  def new
    @guardian = Guardian.new
  end

  def create
    @guardian = Guardian.new(params[:guardian])
    if @guardian.save
      flash[:notice] = ' Parent Record Saved Successfully. Please fill the Additional Details.'
      redirect_to new_student_previous_detail_path(@student)
    else
      flash.now[:error] = 'An error occurred please try again!'
      render 'new'
    end
  end

  def edit
  end

  private
  def set_student
    @student = Student.find(params[:student_id])
  end

end

3-previous_details_controller.rb

class PreviousDetailsController < ApplicationController
  before_filter :set_student, only: [:new, :create]
  def index
    @previous_detail = PreviousDetail.all
  end

  def show
    @previous_detail = PreviousDetail.find(params[:id])
  end

  def new
    @previous_detail = PreviousDetail.new
  end

  def create
    @previous_detail = PreviousDetail.new(params[:previous_detail])
    if @previous_detail.save
      flash[:notice] = 'Record Saved Successfully.'
      # redirect_to user profile page
    else
      flash.now[:error] = 'An error occurred please try again!'
      redirect_to '/student/admission1'
    end
  end

  def edit

  end

  private
  def set_student
    @student = Student.find(params[:student_id])
  end
end

4-学生.rb

class Student < ActiveRecord::Base
  after_create :add_to_users

  belongs_to :user
  accepts_nested_attributes_for :guardians
  has_one :previous_detail
  accepts_nested_attributes_for :previous_detail

  def add_to_users
    new_user = User.new
    new_user.user_name = self.first_name
    new_user.first_name = self.first_name
    new_user.last_name = self.last_name
    new_user.email = self.email
    new_user.password = "123456"
    new_user.password_confirmation = "123456"
    new_user.user_type_id = 3
    new_user.save
  end
end

此回调用于在用户模型中创建学生。

我如何告诉 previous_details_controller.rb 转到用户个人资料页面(刚刚创建并使用用户模型创建的学生after create :add_to_user)?

4

1 回答 1

0

我已经解决了我的问题,如果需要,我会添加这个 slolution 可能对任何人都有帮助。

1-将回调方法修改add_to_user为以下

def add_to_users
    new_user = User.new
    new_user.user_name = self.first_name
    new_user.first_name = self.first_name
    new_user.last_name = self.last_name
    new_user.email = self.email
    new_user.password = "123456"
    new_user.password_confirmation = "123456"
    new_user.user_type_id = 3
    self.user_id = new_user.id
    new_user.save
    t = Student.find(self.id)
    t.user_id = new_user.id
    t.save
  end

因此它会将学生添加到用户模型中,并且还会更新学生模型中的 user_id 以在两者之间进行匹配。我还修改了上面回调的名称,after_create :add_to_users , on: :create以确保它只会在创建操作时引发。

2-修改previous_details_controller中的创建操作,我需要将用户配置文件重定向到以下内容

def create
    @previous_detail = PreviousDetail.new(params[:previous_detail])
    if @previous_detail.save
      flash[:notice] = 'Record Saved Successfully.'
      redirect_to user_path(@student.user_id)
    else
      flash.now[:error] = 'An error occurred please try again!'
      redirect_to '/student/admission1'
    end
  end

键入所有这些后,我会将我重定向到属于新添加到系统的学生的用户个人资料页面。

于 2013-09-13T02:17:24.983 回答