问题
I am working in Rails 3.2.11 and I cannot figure out why this button will not render per the API documentation. Specifically, I cannot get the data-remote attribute to render correctly. Is there a bug in my code?
Right now this code:
<%= button_to "Test", :action => "test", :remote => true, :form_class => "test_button" %>
yields this HTML:
<form action="/cloud_status/test?form_class=test_button&remote=true" class="button_to" method="post">
Per the API specs it should render this:
<form action="/cloud_status/test" class="test_button" remote="true" method="post">
What am I missing?
回答1:
I believe the documentation is actually incorrect here in some of the examples. The way to get the output you are looking for is:
<%= button_to "Test", { :action => "test" }, { :remote => true, :form_class => "test_button" } %>
The :remote
and :form_class
should be part of the html_options
hash, which is the third parameter of the button_to
method.
The second parameter can either be a String
or Hash
. When it's a String
it's treated as a URL and when it's Hash
it is passed to url_for
to build the appropriate URL.
来源:https://stackoverflow.com/questions/14735591/why-is-this-button-to-rendering-incorrectly-in-rails-3-2-11