我有以下控制器: 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- student.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
创建的学生)?
答案 0 :(得分:0)
我已经解决了我的问题,如果需要的话,我会添加标题可能对任何人都有帮助。
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
输入完所有内容后,将我重定向到用户的个人资料页面,该页面属于新添加到系统中的学生。