可能是 NOOB 问题。我有一个应该发出 PUT 的表单,但它以 GET 的形式出现。不知道我要去哪里错了。
路线(来自 Rake 路线):
PUT /jira/jira_projects/:id(.:format) {:action=>"update", :controller=>"jira/jira_projects"}
使用 Java 脚本的表单:
<h1><%= @title %></h1>
<form action="/jira/jira_projects/" method="put">
<div id="home">
<div class="yui-g">
<div class="panel">
<h2>Project Details</h2>
<div class="panelBody">
<div id="project" class="innerPanelBody">
<input type="hidden" name="id" id="id" value="<%= @project.id %>">
<label for="name">Name:</label>
<input type="text" name="jira_name" id="name" value="<%= @project.jira_name %>">
<br>
<br>
<label for="application_ids">Applications Name:</label>
<select id='application_ids' name='death_burrito_application_ids[]' class="graph" multiple="multiple" size="5">
<% @all_applications.each do |a| %>
<% if @applications.detect {|x| x.id == a.id } %>
<option selected="selected" value="<%= a.id %>"><%= a.death_burrito_name %>
<% else %>
<option value="<%= a.id %>"><%= a.death_burrito_name %>
<% end %>
<% end %>
</select>
<br>
<br>
<input type="submit" value="Update" class="submit_button">
<input type="button" class="submit_button" name="Cancel" value="Cancel" onclick="window.location = '../../../' " />
<br>
</div>
</div>
</div>
</div>
</div>
$(document).ready(function() {
function showValues() {
var str = $("form").serialize();
$("#results").text(str);
}
$("select").change(showValues);
$('#submit_button').click(function() {
showValues();
window.location = "/jira/jira_projects/" + $("#results").text();
return false;
});
});
日志中的条目:
Started GET "/jira/jira_projects/?id=3&jira_name=ApplyWeb&death_burrito_application_ids%5B%5D=32&death_burrito_application_ids%5B%5D=39&death_burrito_application_ids%5B%5D=40" for 127.0.0.1 at Thu Aug 25 17:07:28 -0700 2011
Processing by Jira::JiraProjectsController#index as HTML
最佳答案
您只能在 HTML 表单中使用 GET 或 POST:
http://www.w3.org/TR/html4/interact/forms.html#h-17.3
对于 Rails,在您的 html 表单中使用 POST,然后包含一个名为“_method”和值为“put”的隐藏输入:
<input type="hidden" name="_method" value="put" />
这也是您发出 DELETE 的方式(将值中的“put”更改为“delete”)。
关于javascript - Ruby on Rails 问题,表单应该发出 PUT,GET 却通过了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7199182/