我是一個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你的幫助。
使用形式,而不是形式的標籤 –
不,當你有多個記錄的form_for不被使用。 form_for只是一個記錄。 Form_tag是解決方案,但面臨路由問題。 – Means