在单个页面上提交多个表单 - 只想提交一个,但它是全部提交

时间:2011-04-17 00:59:50

标签: jquery ruby-on-rails ajax ruby-on-rails-3

好的,所以这是一个非常奇怪的问题,它有很多可能的原因,所以我会尝试包含所需要回答的所有内容,但如果还有其他什么只是告诉我,我会发布它< / p>

所以我有一个应用程序列出'愿望'基本上只是一个博客/帖子模型。每个愿望都有很多选票,投票属于愿望。在我的索引页面上,我想列出所有的愿望和相应的投票,一个允许投票的按钮,以及一个提交新愿望的表格

这是_new愿望部分

<%= form_for( @wish, :remote => true) do |f| %>
<div class="field">
    <%= f.label :title, 'I Wish UChicago Had' %>
    <%= f.text_field :title %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

它是远程的,因为我想使用ajax在索引页面上发布它 这是我列出愿望的方式,在一个名称,投票和投票按钮的表格中

<tr>
<td> <%= w.title       %> </td>
<td> <%= w.author      %> </td>
<td> <%= w.count_votes %> </td>
<td> <%= render 'vote' %> </td>
<tr>

最后投票部分

<%= form_for(@vote) do |f| %>
  <div class="fields">
    <%= f.hidden_field :wish_id, :value => @w.id%>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

现在问题就出现了,这是我尝试创建新愿望时的rails日志的副本

Started POST "/wishes" for 127.0.0.1 at 2011-04-16 19:44:05 -0500
  Processing by WishesController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"FnrT95FCdv5fJs8AoxIMhrRjBk79wmWJ2s/RdsB43Pg=", "wish"=>{"title"=>"paul"}}
  AREL (0.7ms)  INSERT INTO "wishes" ("title", "body", "author", "created_at", "updated_at") VALUES ('paul', NULL, NULL, '2011-04-17 00:44:05.190203', '2011-04-17 00:44:05.190203')
Rendered wishes/create.js.erb (0.4ms)
Completed 200 OK in 32ms (Views: 8.2ms | ActiveRecord: 0.7ms)


Started POST "/votes" for 127.0.0.1 at 2011-04-16 19:44:05 -0500
  Processing by VotesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"FnrT95FCdv5fJs8AoxIMhrRjBk79wmWJ2s/RdsB43Pg=", "vote"=>{"wish_id"=>"1"}}
  Wish Load (0.5ms)  SELECT "wishes".* FROM "wishes" WHERE "wishes"."id" = 1 LIMIT 1
  AREL (0.3ms)  INSERT INTO "votes" ("weight", "wish_id", "created_at", "updated_at") VALUES (1, 1, '2011-04-17 00:44:05.321513', '2011-04-17 00:44:05.321513')
Redirected to http://localhost:3000/
Completed 302 Found in 62ms


Started GET "/" for 127.0.0.1 at 2011-04-16 19:44:05 -0500
  Processing by WishesController#index as HTML
  Wish Load (54.4ms)  SELECT "wishes".* FROM "wishes"

如您所见,愿望会正确发布到/wishes,甚至可以正确呈现create.js!但它继续前进并呈现/votes帖子!并且像往常一样呈现为html。

为什么要提交这两种表格?我只是想提交一个或另一个?

修改 _ 这是页面上输出的HTML

<table border="1">
<tr>
<td> paul </td>
<td>  </td>
<td> 1 </td>
<td> <form accept-charset="UTF-8" action="/votes" class="new_vote" id="new_vote" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="FnrT95FCdv5fJs8AoxIMhrRjBk79wmWJ2s/RdsB43Pg=" /></div>
  <div class="fields">
    <input id="vote_wish_id" name="vote[wish_id]" type="hidden" value="1" />
  </div>
  <div class="actions">
    <input id="vote_submit" name="commit" type="submit" value="Create Vote" />
  </div>
</form> </td>
<tr>
<tr>
<td> paul kaplan </td>
<td>  </td>
<td> 0 </td>
<td> <form accept-charset="UTF-8" action="/votes" class="new_vote" id="new_vote" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="FnrT95FCdv5fJs8AoxIMhrRjBk79wmWJ2s/RdsB43Pg=" /></div>
  <div class="fields">
    <input id="vote_wish_id" name="vote[wish_id]" type="hidden" value="2" />
  </div>
  <div class="actions">
    <input id="vote_submit" name="commit" type="submit" value="Create Vote" />
  </div>
</form> </td>
<tr>
</table>

<form accept-charset="UTF-8" action="/wishes" class="new_wish" data-remote="true" id="new_wish" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="FnrT95FCdv5fJs8AoxIMhrRjBk79wmWJ2s/RdsB43Pg=" /></div>


  <div class="field">
    <label for="wish_title">I Wish I Had</label>
    <input id="wish_title" name="wish[title]" size="30" type="text" />
  </div>
  <div class="actions">
    <input id="wish_submit" name="commmm" type="submit" value="submit" />
  </div>
</form>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

它已经有一段时间了,但为了完整性,如果有其他人遇到这个问题,我想我应该提出我的答案。基本上上面的评论是正确的,我有两个具有相同ID的表单,所以它们都提交它们,并且javascript搞砸了。