Ruby on Rails -双重渲染错误-重定向并返回

hgqdbh6s  于 12个月前  发布在  Ruby
关注(0)|答案(1)|浏览(74)

错误:AbstractController::DoubleRenderError(在此操作中多次调用了渲染和/或重定向。请注意,您只能调用render或redirect,并且每个操作最多一次。还要注意,重定向和渲染都不会终止动作的执行,所以如果你想在重定向后退出动作,你需要做一些类似“redirect_to(...)and return”的事情。
更新时,模型检查模板是否存在?,如果为true,则下载报告并重定向到父对象。否则,就重定向。
我在“if”语句的切线侧使用了“and return”,那么为什么它不能像预期的那样工作呢?

def update
  template = resource.check_changes(params[:well_master])
  paramString = resource.to_json
  
  update! do |format|
    if resource.errors.present?
      return render :edit
    else
      if template != ''
        generateWellLetter(template, 'Rpt_Farm_Name', paramString) 
      else
        rec = resource.PERMIT
        format.html { redirect_to admin_well_master_path(rec) }
      end
    end
  end
end
 
def generateWellLetter(template, letterName, params)
  @URL = "#{ENV["API_HOST"]}/api/Document/Generate/#{template}"
  response = HTTParty.post(@URL,
     :body => params,
     :headers => { "Content-Type" => "application/json" })

  Zip::InputStream.open(StringIO.new(response)) do |io|
    while entry = io.get_next_entry
      report = io.read
      send_data report, :filename => letterName + '.docx', :type => 'docx'
    end
  end

  respond_to do |format|
    rec = resource.PERMIT
    format.html { redirect_to admin_well_master_path(rec) } and return
  end
end
7rtdyuoh

7rtdyuoh1#

你渲染的次数太多了,我建议把所有的渲染调用都集中在一起。

# send_data is like render, you can only call it once
while entry = io.get_next_entry
  report = io.read
  send_data report, filename: letterName + ".docx", type: "docx"
end
# can't send_data and also render
#
# can't use respond_to and respond_with (from inherited_resources -> responders)
# that's what `update!` method is for
#
# returning inside of the respond_to block means 
# `redirect_to admin_well_master_path(rec)` never runs
respond_to do |format|
  rec = resource.PERMIT
  format.html { redirect_to admin_well_master_path(rec) } and return
end

如果你使用的是继承的资源,那么你可能应该利用它,否则,它是没有帮助的,你应该只使用respond_to块与自己的逻辑。

def update
  template = resource.check_changes(params[:well_master])
  update! do |success, failure|
    # failure - already renders :edit so it's not needed
    success.html do
      if template.present?
        generate_well_letter(template, resource.to_json) do |report|
          send_data report, filename: "Rpt_Farm_Name.docx", type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        end
      else
        redirect_to admin_well_master_path(resource.PERMIT)
      end
    end
  end
end

def generate_well_letter(template, json)
  url = "#{ENV["API_HOST"]}/api/Document/Generate/#{template}"
  response = HTTParty.post(url, body: json, headers: {"Content-Type" => "application/json"})

  # either you're sending the whole zip file or just one file from the zip file
  Zip::InputStream.open(response) do |io|
    entry = io.get_next_entry
    report = entry.get_input_stream.read
    yield report
  end
end
  • https:github.com/activeadmin/inherited_resources#overwriting-actions*

相关问题