当我第二次点击下载链接时,发生了错误 .
这是日志文件:
AbstractController :: DoubleRenderError(在此操作中多次调用渲染和/或重定向 . 请注意,您可能只调用渲染或重定向,每次操作最多一次 . 另请注意,重定向和渲染都不会终止执行操作,因此,如果您想在重定向后退出操作,则需要执行类似“redirect_to(...)并返回”的操作 . )
在 redirect_to
函数之后添加"and return"之后,代码可以正常工作 .
我不明白 . 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