我有以下控制器: 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
)?