weixin_33728708 2015-06-18 00:00 采纳率: 0%
浏览 10

Rails操作未重定向

I have my rails create action:

 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

When I post to this action, the @assessment is getting created in the database (as well as the answers), and the log shows:

Redirected to http://localhost:3000/assessments
Completed 302 Found in 54ms (ActiveRecord: 15.8ms)

But the view in my browser is not changing. How can I get it to redirect?

FYI this is how I am submitting the form data:

$("#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");
            }
           });



  });
  • 写回答

2条回答 默认 最新

  • weixin_33695450 2015-06-18 00:32
    关注

    If it is an AJAX post, you cannot use Rails redirect_to. You have to use JavaScript to do the redirect. This stackoverflow question might give you the answer.

        format.html { redirect_to assessments_path }
        format.js { render :js => "window.location.href = ('#{assessments_path)}');"}
    
    评论

报告相同问题?

悬赏问题

  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了