Rails的DoubleRenderError。为什么重定向两次?

时间:2016-11-03 02:21:28

标签: ruby-on-rails redirect

当我第二次点击下载链接时,发生了错误。

这是日志文件:

  

AbstractController :: DoubleRenderError(在此操作中多次调用渲染和/或重定向。请注意,您可能只调用渲染或重定向,每次操作最多一次。另请注意,重定向和渲染都不会终止执行动作,所以如果你想在重定向后退出一个动作,你需要做一些类似" redirect_to(...)和返回&#34 ;.)

我添加"并返回"在redirect_to函数之后,代码可以正常工作。

我不明白。 redirect_to之后没有代码。重定向后,动作是否应该终止?

class StatisticsController < ApplicationController
      def getForms
        unless File.exists? "public/downloads/#{session[ "yearSelected" ]}.csv"
          # Database Aggregation 
          new_students = {}
          new_students[ "CP" ] = Student.where([ "first_tamu_term like ? and prim_deg_maj_1 like ? and prim_deg like ?", session[ "yearSelected" ]+"%", "CP%", "M%" ]).count
          new_students[ "CE" ] = Student.where([ "first_tamu_term like ? and prim_deg_maj_1 like ? and prim_deg like ?", session[ "yearSelected" ]+"%", "CE%", "M%" ]).count

          prior_students = {}
          prior_students[ "CP" ] = Student.where([ "first_tamu_term like ? and prim_deg_maj_1 like ? and prim_deg like ?", (session[ "yearSelected" ].to_i-1).to_s+"%", "CP%", "M%" ]).count
          prior_students[ "CE" ] = Student.where([ "first_tamu_term like ? and prim_deg_maj_1 like ? and prim_deg like ?", (session[ "yearSelected" ].to_i-1).to_s+"%", "CE%", "M%" ]).count

          # Generate .csv file containing all the statistics
          CSV.open("public/downloads/#{session[ "yearSelected" ]}.csv", "wb") do |csv|
            csv << ["", "CS", "CE"]
            csv << ["Number of newly-admitted masters students", new_students[ "CP" ].to_s, new_students[ "CE" ].to_s]
            csv << ["Prior Year", prior_students[ "CP" ].to_s, prior_students[ "CE" ].to_s]
          end
        end

        @file_names = {:form1 => "The form for the number of masters students in #{session[ "yearSelected" ]}"}

        # render the page of downloading links
      end

      def getForm1
        Thread.new do
          send_file "public/downloads/#{session[ "yearSelected" ]}.csv", type: 'text/csv'
        end

        redirect_to statistics_getForms_path and return
      end
    end

getForms.html.haml:

%html
  %head
    %title Taulbee survey Statistics
    :css
      .heading{
        color:white;
        background-color:#500000;
        height:50px;
        margin:0;
        padding:0;
        font-size:40px;
      }

      .centered{
        text-align:center;
      }

  %body
    %h1.centered.heading Taulbee Survey
    %p
      %a{:id => "Home", :href => site_index_path}
        %img{:height => "30", :src => "/images/homepageicon.jpg", :width => "30"}/
      %a{:id => "Back", :href => site_studentFilterSelection_path}
        %img{:height => "30", :src => "/images/previous-icon.png", :width => "30"}/

    = link_to @file_names[ :form1 ], statistics_getForm1_path

0 个答案:

没有答案