2017-01-03 36 views
1

我是一個Rails新手,並努力顯示單選按鈕檢查特定記錄基於其布爾值在數據庫(字段名稱是'主要')。顯示所有表條目後,用戶可以檢查另一個單選按鈕,並且必須再次在數據庫中進行更新。因此,基本上任何一個附件都只能是主要的。無論我用form_tag做什麼,最多隻能顯示頁面,但根本沒有表格。Rails 5 form_tag與表和單選按鈕

附件/ index.html.erb:

<% form_tag user_attachments_path do %> 
    <table class="table table-bordered table-striped"> 
    <thead> 
     <tr> 
     <th> Select one </th> 
     <th> Attachment Name </th> 
     <th> Creation Date </th> 
     <th> Delete Attachment </th> 
     </tr> 
    </thead> 
    <tbody> 
     <% @attachments.each do |attachment| %> 
     <%=hidden_field_tag :main, 'attachment.main' %> 
     <tr> 
      <td><% radio_button_tag "attachment_id", attachment.id, attachment.main %> </td> 
      <td><%= attachment.attach.file.basename %></td> 
      <td><%= attachment.posting_date %></td> 
      <td><%= button_to "Delete", user_attachment_path(current_user, attachment), method: "delete", data: { confirm: 'Are you sure?' }, class: "btn btn-danger btn-outline btn-md" %></td> 
     </tr> 
     <% end %> 
    </tbody> 
    </table> 
    <br> 
    <%= submit_tag "Select Main", :class =>'button' %> 
<% end %> 

的routes.rb -

post :attachments, to: "attachments#update", as: :attachments 

而且,做我訪問新檢查收音機的價值像這樣在我的附件#更新,因爲附件是一個局部變量,不確定它是否在範圍之外index.html

if params[:attachment][:main] == 'checked' 

感謝您l你的幫助。

+0

使用形式,而不是形式的標籤 –

+0

不,當你有多個記錄的form_for不被使用。 form_for只是一個記錄。 Form_tag是解決方案,但面臨路由問題。 – Means

回答

0

erb需要修改才能顯示。目前它只執行代碼。 This可能會有所幫助。

<% form_tag user_attachments_path do %> 

應該

<%= form_tag user_attachments_path do %> 
+0

是的,我已經想通了,但基本問題是與form_tag指向一個非RESTful方法說update_main在控制器類似於這個railscasts:http://railscasts.com/episodes/165-edit-multiple?視圖= asciicast。其實是一些form_tag和路線 – Means

+0

問題是'我用form_tag做什麼,最多隻能顯示頁面,但沒有表格',我回答了這個問題。你可能想爲你的其他問題開始一個新的線程。如果你點擊提交按鈕,會發生什麼?查看日誌,您將看到params散列中傳遞的內容以及它如何格式化。此外,radio_button_tag erb缺少等號。 tbh,我不確定我是否理解你想達到的目標,所以我會用我的假設發表另一個答案。 – margo