使用 rails 3 中的表单按钮覆盖 response_to 格式 [英] Override the respond_to format with form button in rails 3

查看:45
本文介绍了使用 rails 3 中的表单按钮覆盖 response_to 格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组报告,这些报告使用 Rails 调用respond_to"以各种格式显示,这样如果 URL 以 CSV 或 JSON 结尾,就会以该格式生成报告.

I have a set of reports that are displayed in various formats using the Rails call "respond_to", such that if the URL ends in CSV or JSON, the report is generated in that format.

我请求制作一个下载按钮,以便更轻松地获取报告,但由于报告具有日期范围等自定义设置,因此我需要能够提交一个表单并在表单中指定响应格式.这可能吗?怎么办?

I had a request to make a download button to make grabbing reports easier, but since the reports have customizations like date ranges, I need to be able to submit one form and specify a response format in the form. Is this possible? How can it be done?

表格代码:

<%= form_tag('', method: 'get') do %>
    <%= hidden_field_tag('s',params[:s]) %>
    <%= select_tag "date_interval", options_from_collection_for_select(Admin::ReportController::DATE_INTERVALS.to_a, 'first', 'last') %>
    <%= label_tag('start_at','Start at') %> <%= text_field_tag('start_at', @start_at, class: 'datetimeselect') %>
    <%= label_tag('end_at','End at') %> <%= text_field_tag('end_at', @end_at, class: 'datetimeselect') %>
    <script>
        $('.datetimeselect').datetimepicker({format: "Y-m-d H O"});
    </script>
    <%= button_tag( 'HTML', :value => 'html', :name => 'run' ) %>
    <%= button_tag( 'CSV', :value => 'csv', :name => 'run' ) %>
    <%= button_tag( 'JSON', :value => 'json', :name => 'run' ) %>
<% end %>

请注意上面的 3 个按钮标签,其中值是格式,参数run"将用于覆盖基于路由的格式.

Note the 3 button tags above where the value is the format and the param 'run' would be used to override the route-based formats.

响应代码(在控制器中生成报告后运行):

Respond-to code (runs after report is generated in the controller):

  def format_results
    respond_to do |format|
      format.html {
        # default render of HTML table
      }
      format.json {
        render json: { results: @results[:results], header: @results[:header], name: @results[:name], stats: { rows: @results.count } }, layout: false
      }
      format.csv {
        render text: report_to_csv( @results ), content_type: 'text/csv', layout: false
      }
    end
  end

当我在 URL 中指定扩展名时,这是可行的,我正在寻找一种使用上面名为run"的按钮中的按钮值来覆盖它的方法.

When I specify the extension in the URL, that works, what I'm looking for is a way to override that using a button value in the buttons named "run" above.

先谢谢你!

推荐答案

现在我终于可以回答我自己的问题了.所以答案如下:

I can finally respond to my own question, now. So here's the answer:

我想出了如何实现这一点.您可以将按钮的名称设置为格式",并将值设置为要附加到 URL 的扩展名.

I figured out how this can be accomplished. You can set the name of the buttons to be "format" and set the value to the extension that you would append to the URL.

 <%= button_tag( 'HTML', :value => 'html', :name => 'format' ) %>
 <%= button_tag( 'CSV', :value => 'csv', :name => 'format' ) %>
 <%= button_tag( 'JSON', :value => 'json', :name => 'format' ) %> 

简单地改变 params[:format] 是行不通的.您必须在操作运行之前将其传入.

Simply changing the params[:format] doesn't work. You have to pass it in before your action runs.

这篇关于使用 rails 3 中的表单按钮覆盖 response_to 格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆