【问题标题】:Rails - Multiple entries for a single attributeRails - 单个属性的多个条目
【发布时间】:2015-12-30 08:36:01
【问题描述】:

我正在使用 Rails 4 制作应用程序。我使用简单表单。

我有一个档案模型和一个资格模型。

这些关联是:

profile.rb

belongs_to :profile

资格.rb

has_many :qualifications  

我的个人资料视图中有一个表单,其中包括我的资格视图中的表单的一部分。

个人资料#form

<%= simple_form_for(@profile) do |f| %>
            <%= f.error_notification %>

              <div class="form-inputs">

          <div class="row">

            <div class="intpol2">
              Your professional qualifications
            </div>

              <%= render 'qualifications/form', f: f %>     

          </div>

资格#form

<%= simple_fields_for :qualification do |f| %>

  <div class="form-inputs">


    <div class="row">
        <div class="col-md-6">
            <%= f.input :title, :label => "Your award" %> 
        </div>

        <div class="col-md-6">


        </div>


    </div>

    <div class="row">
        <div class="col-md-6">
            <%= f.input :level,   collection: [ "Bachelor's degree", "Master's degree", "Ph.D", "Post Doctoral award"] %>
        </div>


        <div class="col-md-6">
        <%= f.input :year_earned, :label => "When did you graduate?", collection: (Date.today.year - 50)..(Date.today.year) %>
        </div>

  </div>

用户可能拥有多个学位。我想添加一个字段,它是一个按钮,上面写着“添加另一个资格”,然后可以使用一组新的资格表单字段。

我发现这篇文章试图做一些稍微不同的事情。我不想要 10 组空白的表单域(它会使表单看起来太长)。

Creating multiple records for a model in a single view in Rails

还有其他方法可以实现吗?

【问题讨论】:

    标签: ruby-on-rails simple-form


    【解决方案1】:

    您将寻找名为cocoon 的宝石;您也可以观看this Railscast (Nested forms),它已经过时了,但仍然很好地解释了结构。


    模式很简单,但需要一些额外的部分:

    1. 有一个调用控制器的 ajax 按钮
    2. 控制器需要返回一个表单并构建fields_for
    3. 您将使用 JS 将新的 fields_for 附加到原始表单中

    最大的问题是你的新fields_forid - 这个模式的新实现使用child_index: Time.now.to_i

    我已经写过这个here

    这是一个新版本:


    阿贾克斯

    首先,你需要一个“Add Qualification”按钮,它通过ajax链接到你的控制器:

    #app/views/profiles/_form.html.erb
    <%= simple_form_for(@profile) do |f| %>
        <%= f.error_notification %>
        <div class="form-inputs">
           <div class="row">
             <div class="intpol2">Your professional qualifications</div>
             <%= render 'qualifications/form', f: f %>     
           </div>
        </div>
           <%= button_to "+", new_profile_path, method: :get %>
    <% end %>
    

    控制器

    这将通过 new 控制器方法,我们应该能够设法返回 ajax 请求的特定响应:

    #app/controllers/profiles_controller.rb
    class ProfilesController < ApplicationController
       respond_to :js, :html, only: :new
       def new
          @profile = Profile.new
          @profile.qualifications.build
          respond_with @profile #-> will invoke app/views/profiles/new.js.erb
       end
    end
    

    响应

    一旦您的new.js.erb 被触发,我们需要构建新的HTML 表单,提取fields_for 并将其附加到您的视图中:

    #app/views/profiles/new.js.erb
    var fields_for = $("<%=j render "profiles/form" %>").html(); //-> might need tweaking
    $(fields_for).appendTo("#new_profile.form-inputs");
    

    child_index

    您还应该更改您的 qualifications/form 以包含 child_index

    #app/views/qualifications/form.html.erb
    <%= simple_fields_for :qualifications, child_index: Time.now.to_i do ...
    

    子索引用于表示fields_for 元素中的index。在我们的例子中(因为我们正在制作所有新记录),这并不重要。使用Time.now.to_i 可确保每次都有一个完全唯一的id


    最后,您需要确保您正在调用:

     <%= simple_fields_for :qualifications ... %> 
    

    ... 复数

    【讨论】:

    • 嗨 Rich,非常感谢。这一切都很清楚,很有帮助,而且很有意义。在我使用茧之前。我试着按照你的步骤。我遇到的问题是我想编辑现有配置文件以添加新资格,我收到 错误。如果我将其更改为更新,那么我是否需要在更新字段中重复控制器操作,或者它是否知道在新操作中查找它?
    • 我已经颠倒了上述步骤并尝试使用 cocoon,但是: 给出了一个未定义的方法` #<0x007fe70a4b38d8>
    【解决方案2】:

    看来您必须使用嵌套形式。你必须尝试你的链接教程,因为我也会使用它。对于另一个教程,您可以将其用作参考nested_forms-rails-4.2

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多