这不是问题,你需要使用一种叫做正式链接的技巧。而不是按钮,你把一个从提交按钮。下面是助手的代码,我用这个:
def formal_link_to(*args, &block)
options = html_options = name = nil
if block_given?
options = args.first
html_options = args.second
name = capture(&block)
else
name = args.first
options = args.second || {}
html_options = args.third
end
method = html_options.delete(:method) || "POST"
method = method.to_s.upcase
url = url_for(options)
html = "<form class=\"formal-link\" action=\"#{url}\" method=\"post\">"
html += "<input type=\"hidden\" value=\"#{form_authenticity_token}\" name=\"authenticity_token\" />"
html += "<input type=\"hidden\" value=\"#{method}\" name=\"_method\" />"
html += link_to(name, "#", html_options)
html += "</form>"
if block_given?
concat(html)
else
return html
end
end
使用此助手像一个正常的link_to,但你可以通过额外的选项:法第二哈希值。例如:
<%= formal_link_to "Fill in lots", { :action => "fill_in_lots" }, { :method => :post } -%>
备注:1。 当然,这将使整个页面重新加载,但难免不使用JavaScript。 2.我认为行动fill_in_lots暴露于POST请求。在GET的情况下,你可以使用普通的link_to helper。