這不是問題,你需要使用一種叫做正式鏈接的技巧。而不是按鈕,你把一個從提交按鈕。下面是助手的代碼,我用這個:
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。