2017-07-02 65 views
-1

在导轨,有一种方法(在控制器中),用于:滑轨创建文件和渲染

  1. 创建文件

  2. render视图或模板到该文件

  3. redirect_torender另一种观点认为

我已经试过各种结构的,但要得到同样的错误:渲染和/或重定向在这个动作中被多次调用。请注意,您只能调用渲染或重定向,并且每次最多只能调用一次。

否则;是否有可能render模板或视图到文件而不显示该模板/视图?

thnx!

代码:

def get_report 

# part 1: create and render file for use with phantomjs 

File.new('./vendor/assets/javascripts/graph_disk1.json','w') {|f| f.write(render "reports/disk", :layout => false)} 
system `phantomjs ./vendor/assets/javascripts/highcharts-convert.js -infile ./vendor/assets/javascripts/graph_disk1.json -outfile ./app/assets/images/chart01.png -options ./vendor/assets/javascripts/resources.json`  

# part 2: create odf-report and use image created bij phantomjs/highcharts-convert 

report = ODFReport::Report.new("#{Rails.root}/app/report_templates/PSC2_KalScanV0.odt") do |r| 
    r.add_image :graphd1, "#{Rails.root}/app/assets/images/chart01.png" 
    send_data report.generate, type: 'application/vnd.oasis.opendocument.text', 
         disposition: 'attachment', 
          filename: 'report.odt' 
    end 

的2份各工作,而不是调用时喜欢这个(在1个动作/控制器)。

+1

请张贴代码! – Pavan

+0

您的描述很混乱。你想创建什么文件?通过渲染该文件的视图是什么意思? – Maxence

+0

你可以编辑你的问题并在上面插入代码(在你的代码下有编辑按钮 – widjajayd

回答

0

好像你试图创建自定义路线与渲染等不同的文件比Rails的方式,让我给你的样品的情况下,例如,你有客户端控制器,但你要创建自定义的方法和路线,而不是7个标准导轨方式等

rails generate controller clients 

内部的routes.rb

resources :clients do 
    collection { 
    get :check_data # this to get data 
    post :import_data # this to post data 
    } 
} 
# prease remove all other routes for client controller that usually generated with get 

应用程序/控制器内部/ client_controller.rb创建路线2种上述方法

def check_data 
    ... 
    # the default view file is /views/clients/check_data.html.erb 
    # but you may also type like this below to render other file 
    # please note the first thing you must mention controllers name then the file name 
    render "clients/noname.html.erb" 
end 

def import_data 
    ... 
    # 
    # here after client saved, it goes to other path instead of default 
    if @client.save 
    redirect_to courses_path 
    end 
end 
1

的解决方案是总是很容易,一旦你找到它:

相反的:f.write(render "reports/disk", :layout => false)

用途:f.write(render_to_string "reports/disk", :layout => false)

,瞧,没有更多的error