2017-01-03 82 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