0
컨트롤러에서 세션 변수를 세션 [: 부모]으로 사용했고이 변수를 모델에 전달하고 싶습니다. 컨트롤러에서 모델로 매개 변수를 전달하는 방법은 무엇입니까? 당신의 도움은 여기Ruby on Rails에서 컨트롤러에서 모델로 세션 변수에 액세스하는 방법
내 controller.rb 파일
def new
@child = Child.new
@child.pickup_people.build
@classes = [['Infant', 0], ['Toddlers', 1], ['Early learners', 2], ['Pre school', 3], ['Pre kindergarten', 4]]
@parent_types = [['Father', 'father'], ['Mother', 'mother'], ['Other', 'other']]
@martial_status = [['Married', 'married'], ['Single', 'single'], ['Divorced', 'divorced'], ['Separated', 'separated'], ['Widowed', 'widowed'], ['Other', 'other']]
@parent = Parent.new
#parents those will be mapped with child, here mentioned to show in form page after creation
if session[:parent_id] != nil
@parent_sessions = Parent.where(:id => session[:parent_id])
end
end
def create
logger.debug "\033[31mI am here\033[31m"
@child = Child.new(child_params)
if current_user.has_role? :day_care
@child.day_care_id = current_user.day_care.id
elsif current_user.has_role? :director
@child.day_care_id = current_user.director.day_care.id
elsif current_user.has_role? :assistant_director
@child.day_care_id = current_user.assistant_director.day_care.id
elsif current_user.has_role? :teacher
@child.day_care_id = current_user.teacher.day_care.id
end
respond_to do |format|
if @child.save
# logger.debug "\033[31m#{session[:parent_id]}\033[31m"
if session[:parent] != nil
session[:parent].each do |p|
Relative.create(:child_id => @child.id, :parent_id => p["id"].to_i, :parent_type => p["type"])
end
end
####
gflash :success => "Child was succesfully created."
format.html { redirect_to @child }
format.json { render :show, status: :created, location: @child }
session[:parent] = nil
session[:parent_id] = nil
logger.debug "\033[31mdestroyed#{session[:parent_id]}\033[31m"
else
gflash :error => "Unable to create, please try again."
format.html { render :new }
format.json { render json: @child.errors, status: :unprocessable_entity }
end
end
end
감사합니다!
미안 해요, 난 당신의 질문에 문제가 무엇인지 말할 수 없다, 나는 볼 수 없습니다 뭐 잘못 됐어, 너 해봤 어? – martincarlin87
확인 http://stackoverflow.com/a/10237527/707636. 아마도 당신은 약간의 아이디어를 얻을 것입니다. 또한 모델에 전달하고자하는 것과 같은 세부 정보를 제공 할 수 있다면 좋을 것입니다. – Bongs
고마워, 내가 그걸 시도해. – Karthick