Rails 4未定义的本地变量或下载链接的方法

时间:2015-10-01 19:12:04

标签: ruby-on-rails

我一直为#<#:0x0000376c505098>

获取未定义的局部变量或方法`orders_download_template_path'

使用以下代码。我也尝试过download_template_path和@orders_download_template_path(最后一个不会抛出错误,但也没有做任何事情)。

routes.rb中:

  resources :orders do   
    collection do
      post :import
      get  :upload_page, as: 'upload_page'
      get  :search, as: 'search'
      get  :csv_report, as: 'csv_report'
      get  :overdue_csv_report, as: 'overdue_csv_report'
      get  :download_template, as: 'download_template'
    end
  end

orders_controller.rb:

  def download_template
    send_file Rails.root.join('public/upload_template.csv'),
    type: 'application/csv',
    x_senfile: true
  end

视图:

<%= link_to "Blank Upload Template", orders_download_template_path %>   

文件已放置在/ public

2 个答案:

答案 0 :(得分:1)

根据Rails路由指南,您会遇到语法错误。

their site

这应该解决它

<?php
$db = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

if(isset($_POST['submit'])){
    $count=0;
    $url_text=$_POST['content'];
    $urls=explode(",",$url_text);
    if(count($urls)>20){
        echo "Url Should not exceed 20";           
    }else{
        for($j=0;$j<count($urls);$j++){
            if($urls[$j]!=''){
                $url=$urls[$j];
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_TIMEOUT, '180');
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_ENCODING, "");
                $cUrlResponse = curl_exec($ch);
                $httpResponseArr = curl_getinfo($ch);
                curl_close($ch);

                $content = array();
                $new=explode("<td>",$cUrlResponse);
                for($i=1;$i<count($new);$i++){
                    $new_input=explode("</td>",$new[$i]);
                    $content_input=explode(":",$new_input[0]);
                    $content[]=trim($content_input[1]);
                }
                $stmt4 = $db->prepare("insert into example (data1,data2,data3) values ('$content[0]','$content[1]','$content[2]')");
                $stmt4->execute();
                $count++;
            }
            echo $count." Rows Inserted Successfully....";
        }
    }
}?>
<form method="post" method="">
    <textarea name="content"></textarea>
    <br>
    <input type="submit" name="submit" value="Submit" />
</form>

答案 1 :(得分:0)

如果您在控制台上运行rake routes,则应该看到以下行(以及其他行):

 download_template_orders GET    /orders/download_template(.:format)   orders#do

如您所见,如果您在COLLECTION上设置此路线,则路径生成将为download_template_orders_path。

如果您在会员上设置它,您会看到:

 download_template_order GET    /orders/:id/download_template(.:format)  orders#

回答你的问题,正确的路径是download_template_orders_path