Ruby on rails Rails-控制台显示';渲染';但这种改变并没有发生

Ruby on rails Rails-控制台显示';渲染';但这种改变并没有发生,ruby-on-rails,performance,api,controller,request,Ruby On Rails,Performance,Api,Controller,Request,我编写了一个服务模块,它发出一个API请求,返回一个包含错误的字符串或一个包含参数的散列,并将其包含在控制器中 def create @new_params = ApiRequest.create_login(params, customer_secret) #this returns either a string or hash request_error? #if it's a string, it should immediately render :new pa

我编写了一个服务模块,它发出一个API请求,返回一个包含错误的字符串或一个包含参数的散列,并将其包含在控制器中

def create
    @new_params = ApiRequest.create_login(params, customer_secret) #this returns either a string or hash
    request_error? #if it's a string, it should immediately render :new
    params[:login] = @new_params #substitutes the params from the form with the API response
    @login = current_user.logins.new(login_params).save
  end
下面是它在不尝试保存的情况下应该如何重定向

def request_error?
    respond_to do |format|
      if @new_params.is_a? String
        @error = @new_params
        format.html { render :new, notice: @error }
      end
    end
end
问题是,在控制台中,它说它
rendered:new
(或者甚至
重定向到new\u login\u path
——如果我使用它而不是render),但页面甚至没有重新加载,只是什么都没有发生。而且,即使它应该在前面使用render:new退出create操作,它仍然尝试保存@login并失败。

我的猜测是长API请求破坏了操作(对于整个控制器操作>300ms)。要解决此问题,我应该怎么做?

这里的问题是,您正在尝试渲染控制器操作,但没有通过返回来停止控制器操作的进度

考虑替换内联到操作的
request\u error?
方法:

def create
  @new_params = ApiRequest.create_login(params, customer_secret) #this returns either a string or hash
  respond_to do |format|
    if @new_params.is_a? String
      @error = @new_params
      format.html { render :new, notice: @error }
    end
  end
  params[:login] = @new_params #substitutes the params from the form with the API response
  @login = current_user.logins.new(login_params).save
end
由于在条件中呈现时不返回,因此操作将继续并继续保存登录名

为了避免这种情况,在条件中渲染时应返回

def create
  @new_params = ApiRequest.create_login(params, customer_secret) #this returns either a string or hash
  respond_to do |format|
    if @new_params.is_a? String
      @error = @new_params
      format.html { render :new, notice: @error and return }
    end
  end
  params[:login] = @new_params #substitutes the params from the form with the API response
  @login = current_user.logins.new(login_params).save
end
注意,如果继续使用方法
request\u error?
,就不能执行此操作,就像在该方法中返回控制权一样。相反,您需要返回一个布尔值来确定是否已渲染,如:

def request_error?
    respond_to do |format|
      if @new_params.is_a? String
        @error = @new_params
        format.html { render :new, notice: @error }
        return true
      else
        return false
      end
    end
end
然后,如果出现请求错误,请提前返回:

  def create
    @new_params = ApiRequest.create_login(params, customer_secret) #this returns either a string or hash
    return if request_error? 
    params[:login] = @new_params #substitutes the params from the form with the API response
    @login = current_user.logins.new(login_params).save
  end

让我知道这是否有帮助。

这里的问题是,您正在尝试渲染,但没有通过返回来停止控制器操作的进度

考虑替换内联到操作的
request\u error?
方法:

def create
  @new_params = ApiRequest.create_login(params, customer_secret) #this returns either a string or hash
  respond_to do |format|
    if @new_params.is_a? String
      @error = @new_params
      format.html { render :new, notice: @error }
    end
  end
  params[:login] = @new_params #substitutes the params from the form with the API response
  @login = current_user.logins.new(login_params).save
end
由于在条件中呈现时不返回,因此操作将继续并继续保存登录名

为了避免这种情况,在条件中渲染时应返回

def create
  @new_params = ApiRequest.create_login(params, customer_secret) #this returns either a string or hash
  respond_to do |format|
    if @new_params.is_a? String
      @error = @new_params
      format.html { render :new, notice: @error and return }
    end
  end
  params[:login] = @new_params #substitutes the params from the form with the API response
  @login = current_user.logins.new(login_params).save
end
注意,如果继续使用方法
request\u error?
,就不能执行此操作,就像在该方法中返回控制权一样。相反,您需要返回一个布尔值来确定是否已渲染,如:

def request_error?
    respond_to do |format|
      if @new_params.is_a? String
        @error = @new_params
        format.html { render :new, notice: @error }
        return true
      else
        return false
      end
    end
end
然后,如果出现请求错误,请提前返回:

  def create
    @new_params = ApiRequest.create_login(params, customer_secret) #this returns either a string or hash
    return if request_error? 
    params[:login] = @new_params #substitutes the params from the form with the API response
    @login = current_user.logins.new(login_params).save
  end

让我知道这是否有帮助。

这与控制器或请求无关,默认情况下,带有的
表单是用Ajax提交的,为了响应html,我必须指定
local:true
。前面的注释解决了“提前退出控制器操作”的问题。

它与控制器或请求无关,默认情况下,带有
表单是用Ajax提交的,为了响应html,我必须指定
local:true
。前面的注释解决了“提前退出控制器操作”的问题。

Ok,问题是使用Ajax提交表单。Rails 5中带有
的新的
form_默认为
remote:true
,因此为了响应html,您必须指定
local:true
。好的,问题是表单使用Ajax提交。Rails 5中带有
的新的
表单在默认情况下具有
remote:true
,因此为了响应html,您必须指定
local:true
。arg this!这救了我,至少困了一个小时。带有本地需求的表单:真到重定向!哎呀!这救了我,至少困了一个小时。带有本地需求的表单:真到重定向!