Rails form_tag没有响应

时间:2014-03-19 23:59:17

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1

我有一张由于某种原因无响应的表单而且我被卡住了!

我已经关注了RailsCast here,但它基于旧版本的rails。

这是我的观看代码,非常确定我的麻烦在哪里:

<%= form_tag publish_selected_posts_path do %>
    <% @posts_inactive.each do |post| %>
      <tr>
        <td><%= check_box_tag "post_ids[]", post.id %></td>
        <td><%= link_to post.title, post_path(post) %></td>
        <td><%= post.created_at.to_s(:short) %></td>
        <td><%= post.user.email %></td>
        <td><%= post.user.email %></td>
        <td><%= post.state.name %></td>
        <td><%= post.city.name %></td>
        <td><%= post.expire_date.to_s(:short) %></td>
        <td>
            <div>
                <div class="dark" style="float:left;">
                  <%= link_to 'Edit', edit_post_path(post), :class => 'btn btn-mini dark' %> &nbsp;
                </div>
                <div style="float:left;">
                  <%= button_to 'Delete', post_path(post), :method => :delete, :confirm => 'Are you sure?', :class => 'btn btn-mini btn-danger' %>
                </div>
                <div class="dark" style="float:left;">
                     &nbsp; <%= link_to publish_link_text(post), 
                                toggle_publish_post_path(post), 
                                :class => 'btn btn-mini dark' %>
                </div>
                <div style="clear:both;">
                </div>
            </div>
        </td>
      </tr>
    <% end %>   

     </tbody>
    </table>
  <%= submit_tag "Publish Selected", :class => 'btn btn-mini dark' %>
<% end %>   

当我按下提交按钮时,没有任何事情发生......我可以说,一切都应该基于轨道演员,但它只是不起作用。

以下是此表单输出的HTML:

    <form accept-charset="UTF-8" action="/dashboard/posts" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="pwTuSXar01OT26FHf1bMyxKsSQ7MHWn/u3BQ9PmffS4=" /></div>
          <tr>
            <td><input id="post_ids_" name="post_ids[]" type="checkbox" value="29" /></td>
            <td><a href="/posts/29">Another Post</a></td>
            <td>30 Nov 05:03</td>
            <td>guitarjoe02@email.com</td>
            <td>guitarjoe02@email.com</td>
            <td>Alaska</td>
            <td>Anchorage</td>
            <td>28 Jan</td>
            <td>
                <div>
                    <div class="dark" style="float:left;">
                      <a href="/posts/29/edit" class="btn btn-mini dark">Edit</a> &nbsp;
                    </div>
                    <div style="float:left;">
                      <form action="/posts/29" class="button_to" method="post"><div><input name="_method" type="hidden" value="delete" /><input class="btn btn-mini btn-danger" data-confirm="Are you sure?" type="submit" value="Delete" /><input name="authenticity_token" type="hidden" value="pwTuSXar01OT26FHf1bMyxKsSQ7MHWn/u3BQ9PmffS4=" /></div></form>
                    </div>
                    <div class="dark" style="float:left;">
                         &nbsp; <a href="/posts/29/toggle_publish" class="btn btn-mini dark">Publish</a>
                    </div>
                    <div style="clear:both;">
                    </div>
                </div>
            </td>
          </tr>
          <tr>
            <td><input id="post_ids_" name="post_ids[]" type="checkbox" value="28" /></td>
            <td><a href="/posts/28">New Post...</a></td>
            <td>16 Nov 01:09</td>
            <td>guitarjoe02@email.com</td>
            <td>guitarjoe02@email.com</td>
            <td>Alaska</td>
            <td>Anchorage</td>
            <td>14 Jan</td>
            <td>
                <div>
                    <div class="dark" style="float:left;">
                      <a href="/posts/28/edit" class="btn btn-mini dark">Edit</a> &nbsp;
                    </div>
                    <div style="float:left;">
                      <form action="/posts/28" class="button_to" method="post"><div><input name="_method" type="hidden" value="delete" /><input class="btn btn-mini btn-danger" data-confirm="Are you sure?" type="submit" value="Delete" /><input name="authenticity_token" type="hidden" value="pwTuSXar01OT26FHf1bMyxKsSQ7MHWn/u3BQ9PmffS4=" /></div></form>
                    </div>
                    <div class="dark" style="float:left;">
                         &nbsp; <a href="/posts/28/toggle_publish" class="btn btn-mini dark">Publish</a>
                    </div>
                    <div style="clear:both;">
                    </div>
                </div>
            </td>
          </tr>

  </tbody>
</table>
  <input name="commit" type="submit" value="Save changes" /> 
</form> 

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

您的提交标记似乎不在表单中。这将使它实际上不提交表格。

编辑:我想我可能错了......但是代码的狡猾缩进让人难以分辨。你能否清理缩进以使得do / ends(及其包含的代码)更清晰,一目了然?

答案 1 :(得分:2)

啊哈哈!罪魁祸首是你的button_to电话。查看the documentation for button_to。正如您将看到的那样,该帮助程序将自己包装在一个迷你HTML表单中。在您的情况下,它在表单中创建一个表单,invalid

您应该能够使用link_to执行完全相同的操作。因此,button_to最终会看起来像这样:

<%= link_to 'Delete', post_path(post), :method => :delete, :confirm => 'Are you sure?', :class => 'btn btn-mini btn-danger' %>

尝试一下,让我知道它是否有所作为。