2012-12-20 239 views
1

我对Ruby on Rails相当陌生,而且我看起来像一个简单的问题,但我似乎无法弄清楚我做错了什么。我的主页有一个按钮,当你点击按钮时,它应该创建一个包含数据库信息的xml文件。Ruby on Rails路由错误

按钮代码:

<%= button_to "Create Google File", :action => :create_google_file %> 

控制器代码:

class ProductsController < ApplicationController 
    def new 
    end 

    def create_google_file  
     @products = Product.find(:all) 
     file = File.new('dir.xml','w') 
     doc = @products.to_xml 

     file.puts doc 
     file.close 
    end 
end 

我得到的错误是

No route matches {:action=>"create_google_file", :controller=>"products"} 
+0

你能证明你的'routes.rb'文件好吗? – tbraun89

+0

'GoogleMine :: Application.routes.draw做 GET “产品/新” 资源:产品 根: '产品#新' end' –

+0

看一看安德烈亚斯Lyngstad答案,应该帮助你。 – tbraun89

回答

1

添加到您的config/routes.rb中文件

match "/create_google_file" => "products#create_google_file", :as => :create_google_file 

并更改按钮,这

<%= button_to "Create Google File", create_google_file_path %>