ruby-on-rails - 如何重定向到相同控制器动作的另一种格式?
问题描述
我有这个index
方法在我的TasksController
:
def index
@tasks = current_account.tasks
@count = @tasks.length
respond_to do |format|
format.html do
...
end
format.zip do
if @count > 100
flash[:notice] = "Please reduce the number of tasks!"
redirect_to :action => "index", :format => "html"
else
DownloadArchive.call(@tasks)
end
end
end
end
html
如果有超过 100 个任务,我如何呈现我的索引操作的版本?
我上面的代码不起作用。它不是重定向和显示闪存消息,而是下载html
文件。我不明白为什么。如果可以,请你帮助我。
解决方案
格式 zip 将下载一个文件,无论您在块中传递什么。如果您想确定 zip 文件是否甚至可以下载,您需要在处理格式zip
请求之前执行此操作。您可能需要更改视图代码以不显示下载按钮或处理zip
请求的任何内容。
def index
@tasks = current_account.tasks
@count = @tasks.length
if @count > 100
flash[:notice] = "Please reduce the number of tasks!"
redirect_to :index and return
end
respond_to do |format|
format.html do
...
end
format.zip do
DownloadArchive.call(@tasks)
end
end
end
end
推荐阅读
- java - 功能 If/else cannot run correct_app android use Accelerometer
- openshift - 通过模板创建 dockercfg secret
- elixir - **所有**测试完成后如何清理测试数据库?
- python - 从html中提取基于字体系列类型的文本
- java - Java:通过 TCP 从 python 脚本接收包含元组的列表列表
- r - R ftpUplad 错误:无法打开连接
- apache-spark - 用于结构化流的 Elasticsearch spark 连接
- css - sass bootstrap 覆盖菜单代码的正确语法
- python - 尝试安装 python 数据表时出现 C++ 编译器错误
- android - 默认暂停 exoplayer 并响应播放/暂停按钮