Rails 5 form_tag与表和单选按钮

时间:2017-01-03 09:13:26

标签: ruby-on-rails ruby-on-rails-5 form-helpers

我是一个Rails新手,并努力根据DB中的布尔值(字段名称为' main')显示为特定记录检查的单选按钮。显示所有表条目后,用户可以检查另一个单选按钮,并且必须再次在DB中更新。因此,基本上任何一个附件都可以成为主要的。无论我使用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

另外,我是否在附件#update中访问新检查的收音机的值,因为附件是一个局部变量,不确定它是否具有index.html之外的范围

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

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

需要修改erb才能显示。目前它只执行代码。 This可能有帮助。

<% form_tag user_attachments_path do %>

应该是

<%= form_tag user_attachments_path do %>