Rails 3
写了个路由
get "report/index" get "report/show"
rake routes可以知道
report_index GET /report/index(.:format) {:controller=>"report", :action=>"index"} report_show GET /report/show(.:format) {:controller=>"report", :action=>"show"}
于是,就写了个button_to
%h1 Report#show = button_to('click', :action => 'index',:method=>:get)
发现诡异现象,点击就会提示没有路由,地址栏回车就能显示。
我就记着很久之前遇到过这个问题,可是就是想不起来怎么回事。
后来看后台信息和地址栏,发现问题
引用
http://127.0.0.1/report/show?method=get
后台
引用
Started POST "/report/index?method=get" for 192.168.1.130 at 2011-05-19 14:04:41 -0400
ActionController::RoutingError (No route matches "/report/index"):
仔细看看API
<%= button_to "New", :action => "new" %> # => "" <%= button_to "Delete Image", { :action => "delete", :id => @image.id }, :confirm => "Are you sure?", :method => :delete %> # => ""
发现少了个大括号
引用
button_to(name, options = {}, html_options = {})
Generates a form containing a single button that submits to the URL created by the set of options. This is the safest method to ensure links that cause changes to your data are not triggered by search bots or accelerators. If the HTML button does not work with your layout, you can also consider using the link_to method with the :method modifier as described in the link_to documentation.
The generated form element has a class name of button_to to allow styling of the form itself and its children. You can control the form submission and input element behavior using html_options. This method accepts the :method and :confirm modifiers described in the link_to documentation. If no :method modifier is given, it will default to performing a POST operation. You can also disable the button by passing :disabled => true in html_options. If you are using RESTful routes, you can pass the :method to change the HTTP verb used to submit the form.
Options
The options hash accepts the same options as url_for.
There are a few special html_options:
:method - Symbol of HTTP verb. Supported verbs are :post, :get, :delete and :put. By default it will be :post.
:disabled - If set to true, it will generate a disabled button.
:confirm - This will use the unobtrusive JavaScript driver to prompt with the question specified. If the user accepts, the link is processed normally, otherwise no action is taken.
:remote - If set to true, will allow the Unobtrusive JavaScript drivers to control the submit behaviour. By default this behaviour is an ajax submit.