2015-04-29 70 views
1

我試圖讓我刪除多個帖子的基礎上檢查或做另一個操作的窗體。我正在努力從表單中訪問帖子ID。如果我使用f.submit標籤比我的代碼工作正常。但是,當我嘗試使用引導按鈕時,它不起作用。多個表單提交按鈕爲單個窗體軌道4

下面是一個視圖,它具有位於引導表內部的表單,然後位於表外部的提交按鈕。

  <%= form_for :pending_forms, html: {method: 'get'} do |f| %> 
      <table class="table"> 
       <tbody> 
        <% if @pending_posts.any? %> 
          <% @pending_posts.each do |post| %> 
           <tr> 
            <td><%= post.post_name %></td> 
            <td><%= post.created_at %></td> 
            <td><%= check_box_tag 'post_ids[]', post.id %></td> 
           </tr> 
          <% end %> 
        <% end %> 
       </tbody> 
      </table> 
      <button type="submit" class="btn btn-danger">Remove Post</button> 
      <button type="submit" class="btn btn-primary">Go Live</button> 
     <% end %> 

回答

0
if params[:commit] == "Remove Post" 
    PendingPost.where(id: params[:post_ids]).destroy_all 
end 
if params[:commit] == "Go Live" 
    #do some stuff 
end 

最好是寫提交內部的form_tag範圍按鈕。

<%= form_tag do %> 
    <table> 
    <tbody> 
     <% if @pending_posts.any? %> 
      <% @pending_posts.each do |post| %> 
      <tr> 
       <td><%= post.post_name %></td> 
       <td><%= post.created_at %></td> 
       <td><%= check_box_tag 'post_ids[]', post.id %></td> 
      </tr> 
      <% end %> 
     <% end %> 
    </tbody> 
    </table> 
    <button type="submit" class="btn btn-danger">Remove Post</button> 
    <button type="submit" class="btn btn-primary">Go Live</button> 
<% end %> 
+0

這並不能解決我的問題。這仍然無法刪除選定的帖子 – BadProgrammer

+0

在控制器中,你應該寫PendingPost.where(id:params [:post_ids])。destroy_all而不是PendingPost.where(id:params [:commit])。destroy_all –

+0

好吧,但是當我點擊刪除它發送POST時,我的路線設置爲接收GET。 – BadProgrammer

相關問題