带有多个条目的Rails嵌套表单 [英] Rails nested form with multiple entries

查看:136
本文介绍了带有多个条目的Rails嵌套表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Sezzion模型:

attr_accessible  :description
has_many :session_instructors, :dependent => :destroy
has_many :instructors, :through => :session_instructors
accepts_nested_attributes_for :session_instructors
accepts_nested_attributes_for :instructors

Instructor型号:

attr_accessible :bio
has_many :sezzions, :through => :session_instructors
has_many :session_instructors, :dependent => :destroy

SessionInstructor型号:

attr_accessible :instructor_id, :sezzion_id
belongs_to :sezzion
belongs_to :instructor

最后,User模型:

has_many :sezzions
has_many :instructors

我正在尝试为Sezzion创建一个具有SessionInstructor嵌套形式的表单,该表单对Instructors具有多个选择选项.

I'm trying to create a form for Sezzion with nested form for SessionInstructor which has multiple select option for Instructors.

如何执行以下操作:

    SessionInstructor 的嵌套表格
  1. 使用多个选择选项来获取所有选定的Instructorinstructor_id
  2. 隐藏字段,用于将每个选择的指导者传递给创建/更新的session_id
  1. nested form for SessionInstructor
  2. use multiple select option to get all the selected Instructor's instructor_id
  3. hidden field to pass in the created/updated session_id with each select instructor

到目前为止,我有以下代码:

I have the following code as of now:

<%= form_for(@sezzion) do |f| %>

    <%= f.label :description %>
    <%= f.text_area :description %>


    <%= f.label :instructors %>
    <%= fields_for :session_instructors do |f| %>
      <select multiple>
        <% current_user.instructors.each do |instructor| %>
          <option><%= instructor.name %></option>
        <% end %>
      </select>
    <% end %>

    <%= f.submit %>

<% end %>

非常感谢您!

推荐答案

在Rails中,这似乎很难.

This is something that seems ridiculously hard in Rails.

我认为类似的方法可能有用:

I think something like this might work:

<%= f.fields_for :session_instructors do |si| %>
  <%= si.collection_select :instructor_ids, current_user.instructors, :id, :name, multiple: true>
<% end %>

这应该创建一个表单元素,该元素将设置sezzion [session_instructors_attributes] [instructor_ids].

This should create a form element which will set sezzion[session_instructors_attributes][instructor_ids].

尽管我不确定那是否真的是您想要的.我从来没有尝试过使用多重选择.如果它不起作用,您也可以尝试摆脱fields_for而仅使用f.collection_select.如果您愿意使用此复选框,那么我可以肯定地向您展示如何进行选择.

Although I'm not sure if that's actually what you want. I've never tried this using a multi select. If it doesn't work, you could also try getting rid of the fields_for and just using f.collection_select. If you're willing to use a checkbox, I can show you how to do that for sure.

我希望能帮上忙.

这是我通常使用check_box的方式:

Here's how I would usually do it with a check_box:

<%= f.fields_for :session_instructors do |si| %>
  <%= si.hidden_field "instructor_ids[]" %>
  <% current_user.instructors.each do |i| %>
    <%= si.check_box "instructor_ids[]", i.id, i.sezzions.include?(@sezzion), id: "instructor_ids_#{i.id}" %>
    <%= label_tag "instructor_ids_#{i.id}", i.name %>
  <% end %>
<% end%>

有几个陷阱"!用这种方法.编辑模型时,如果取消选择所有复选框,则根本不会发送参数.这就是为什么hidden_field是必需的.另外,您需要确保每个表单元素都有一个唯一的id字段.否则,仅发送最后一个条目.这就是为什么我自己手动设置值的原因.

There are a couple "gotchas!" with this method. When editing a model, if you deselect all checkboxes then it won't send the parameter at all. That's why the hidden_field is necessary. Also, you need to make sure each form element has a unique id field. Otherwise only the last entry is sent. That's why I manually set the value myself.

我复制粘贴然后进行编辑.希望我的语法足够接近,可以在其中使用它.

I copy pasted and then edited. Hopefully I got the syntax close enough where you can get it to work.

最终

根据Sayanee在下面的评论,答案比我想象的要简单:

Per Sayanee's comment below, the answer was a bit simpler than I thought:

<%= f.collection_select :instructor_ids, current_user.instructors, :id, :name, {}, {:multiple => true} %>

这篇关于带有多个条目的Rails嵌套表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆