我的导轨创建动作:
def create
@assessment = Assessment.new(user_id: current_user.id, patient_id: params[:assessment][:patient_id], template_id: params[:assessment][:template_id])
@answers = params[:assessment][:answers]
if @assessment.save
@answers.each do |question, array|
@assessment.answers.create(question_id: question, content: array[0], tracking: array[1])
end
redirect_to assessments_path
else
render :new
end
end
当我发布此操作时,会在数据库中创建@assessment(以及答案),并显示日志:
Redirected to http://localhost:3000/assessments
Completed 302 Found in 54ms (ActiveRecord: 15.8ms)
但我浏览器中的视图没有变化。如何让它重定向?
仅供参考我提交表格数据的方式如下:
$("#new_assessment").find('input[name=commit]').on('click', function(e){
e.preventDefault();
var $inputs = $('#new_assessment :input');
Array.prototype.clean = function(deleteValue) {
for (var i = 0; i < this.length; i++) {
if (this[i] == deleteValue) {
this.splice(i, 1);
i--;
}
}
return this;
};
var template_id = gon.template.id;
var patient_id = gon.patient.id;
var answers = {};
$('.question').each(function() {
var question = $(this).find('.question-field').data('question');
var answer = $(this).find('.question-field').val();
var trackable = $(this).find('.trackable').is(':checked');
if (answer && answer.length > 0 && answer != "undefined") {
answers[question] = [answer, trackable];
}
});
$.ajax({
url: "/assessments",
type: "POST",
data: {assessment:{template_id, patient_id, answers}},
success: function(resp){
console.log("success") },
error: function(err) {
console.log("error");
}
});
});
答案 0 :(得分:1)
在ajax调用中,你必须在控制器中使用js格式
render js: "window.location = '#{assessments_path}'"
答案 1 :(得分:0)
如果是AJAX帖子,则无法使用Rails redirect_to。您必须使用JavaScript来执行重定向。这个stackoverflow question可能会给你答案。
format.html { redirect_to assessments_path }
format.js { render :js => "window.location.href = ('#{assessments_path)}');"}