我是一个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