【问题标题】:Double Render error with rails and ajax callrails和ajax调用的双重渲染错误
【发布时间】:2016-08-21 16:24:10
【问题描述】:

我正在尝试使用 html 和 js 调用呈现不同的内容。我将在 ajax 调用中处理 json 结果。

我不断收到双重渲染错误,说我不能同时使用渲染和重定向。但是它们是针对不同的调用的,所以这不重要吗?

我试图寻找答案,但找不到。

谢谢。

下面是我的代码:

def create
  service = ServiceCall.new.call
  if this
    if abc
      set_flash('success')
    else
      set_flash('notice')
    end

    respond_to do |format|
      format.html { redirect_to(log_in_path) && return}
    end
  elsif that
    respond_to do |format|
      format.html { redirect_to(root_path) && return}
    end
  end

  respond_to do |format|
    format.js { render json: service, status: :ok }
  end
end

【问题讨论】:

    标签: jquery ruby-on-rails ruby-on-rails-4


    【解决方案1】:

    也许return 没有像您期望的那样工作,并且您的块没有在当前方法中执行。它被用作更深层次的参数。所以你不要离开当前的方法。一种可能的解决方案:

    def create
      service = ServiceCall.new.call
      redirect_path = if this
            if abc
              set_flash('success')
            else
              set_flash('notice')
            end
    
            log_in_path
    
          elsif that
             root_path
          end
    
      respond_to do |format|
        format.html { redirect_to(redirect_path) }
        format.js { render json: service, status: :ok }
      end
    end
    

    【讨论】:

    • 我确实有资格使用我的遮阳篷。也许回报不是问题。当格式为js 时,您多次调用respond_to。我的第一个想法是该块只是存储而不是立即执行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多