Rails-茧形宝石-嵌套表格 [英] Rails - Cocoon gem - Nested Forms

查看:73
本文介绍了Rails-茧形宝石-嵌套表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Rails 4制作应用程序.

I am trying to make an app with Rails 4.

我将简单的表单gem用于表单,并尝试将Cocoon gem用于表单的嵌套元素.

I use simple form gem for forms and am trying to use Cocoon gem for nested elements of the forms.

我问了这个问题,它详细解释了我要实现的目标:

I asked this question, which explains what I'm trying to achieve in more detail: Rails - Multiple entries for a single attribute

我有个人资料模型和资格证书模型.关联是:

I have a profile model and a qualifications model. The associations are:

profile.rb

profile.rb

has_many :qualifications  
    accepts_nested_attributes_for :qualifications,  reject_if: :all_blank, allow_destroy: true

qualification.rb

qualification.rb

belongs_to :profile

个人档案控制器在强参数中包括资格属性:

the profiles controller includes the qualification attributes in the strong params:

def profile_params
      params[:profile].permit(:user_id, :title, :hero, :overview, :research_interest, :occupation, :external_profile, 
        :working_languages, :tag_list,
          qualifications_attributes: [:id, :level, :title, :year_earned, :institution, :_destroy] )
    end

我的个人资料表单现在具有:

My profile form now has:

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

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

          </div>
          <div class="row">
            <div class="col-md-6">
              <%= link_to_add_association 'Add a qualification', f: f,  partial: 'qualifications/qualification_fields' %>

            </div>

我的资格认证部分表格现在在我的资格认证视图文件夹中命名为_qualifications_fields.html.erb

My qualifications partial form is now named in my qualification views folder as _qualifications_fields.html.erb

<div class="nested-fields">
<div class="container-fluid">

        <%= simple_fields_for :qualifications 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>


          <div class="row">
                <div class="col-md-6">
                     <!-- link_to_remove_association 'Remove this qualification', f, :f  -->
                </div>

          </div>


          </div>
        <% end %>
</div>  
</div>      

我注释掉了删除链接,因为按原样,我收到此错误:

I commented out the remove link, because as is, I get this error:

undefined method `new_record?' for nil:NilClass

我对Rails报告错误的方式感到困惑-我很少理解这种样式的错误消息,但是我认为这与没有任何资格删除有关.茧可以处理吗?

I'm baffled by the way Rails reports errors- I rarely understand error messages of this style, but I gather it's something to do with there not being any qualifications to remove. Does cocoon handle this?

当我注释掉并重试时,出现此错误,并带有添加关联的链接:

When I comment this out and try again, I get this error with the link to add an association:

undefined method `object' for #<Hash:0x007fe70c501ea0>

我也不知道这条消息是什么意思.

I don't know what this message means either.

因此,请按照Cocoon gem文档中的步骤进行操作(进行更改,因为我不知道如何用haml编写内容;并更改表达式的形式以引用表单生成器'f:f'可以在我的其余表单,所以我猜测文档中显示的表达式可能与haml有关),现阶段我有2个错误:

So, by following the steps in the Cocoon gem docs, (with changes because I don't know how to write things in haml; and changing the form of expression to reference the form builder 'f: f' works across the rest of my forms, so Im guessing that the expression shown in the docs might be something to do with haml), I have 2 errors at this stage:

  1. 添加关联-未定义的方法对象
  2. 删除关联-未定义的方法"new_record?"

我正在努力了解这里发生的事情.

I'm struggling to understand what's going on here.

接受捷运的建议

我将个人资料表格更改为:

I change my profile form to:

    <%= simple_form_for @profile, html: { multipart: true }  do |f| %>
            <%= f.error_notification %>

              <div class="form-inputs">


          <div class="row">

            <div class="intpol2">
              Your professional qualifications
            </div>
            <%= simple_fields_for :qualifications do |f| %>

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

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

               <%= link_to_add_association 'Add a qualification', f, :qualifications, partial: 'qualifications/qualification_fields' %>
            <% end %>
            </div>

          </div>

              <div class="form-actions">
                <%= f.button :submit, "Submit", :class => 'formsubmit' %>
              </div>
        <% end %>
    </div>
  </div>    
</div>      

我将资格字段表格更改为:

I change my qualification fields form to:

<div class="nested-fields">
<div class="container-fluid">

          <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>


          <div class="row">
                <div class="col-md-6">
                    <%= link_to_remove_association 'Remove this qualification', f %>
                </div>

          </div>


          </div>

</div>  
</div>      

保存并重试时,出现此错误:

When I save this and try again, I get this error:

undefined method `new_record?' for nil:NilClass

该错误消息突出显示了这一行:

The error message highlights this line:

            <%= link_to_remove_association 'Remove this qualification', f %>

推荐答案

link_to_add_association 'Add a qualification', f: f,  partial: 'qualifications/qualification_fields'

f: f在这里不正确.第二个参数应该是表单生成器(只是一个对象,而不是哈希),第三个参数是关联的名称,即您的情况下为:qualifications.所以它应该像这样:

f: f is incorrect here. The second parameter should be a form builder (just an object, not a hash) and the third parameter – the name of the association, i.e. :qualifications in your case. So it should look like this:

link_to_add_association 'Add a qualification', f, :qualifications, partial: 'qualifications/qualification_fields'

在这里:

link_to_remove_association 'Remove this qualification', f, :f

:f也是错误的.这里的第三个参数是HTML选项.如果不需要它们,则只需指定2个参数:

:f is also wrong. Third parameter here is HTML options. If you don't need them, then just specify 2 parameters:

link_to_remove_association 'Remove this qualification', f

接下来,_qualification_fields.html.erb不应包含simple_fields_for.此部分用于呈现一个不同的关联对象的字段.当您单击添加限定条件"或您的父对象已经具有某些限定条件时,将显示它. simple_fields_for :qualifications应该放在您的个人资料表格中,并包含render 'qualifications/qualifications_fields', f: flink_to_add_association ....考虑以下示例: https://github.com/nathanvda/cocoon#simpleform

Next, _qualification_fields.html.erb shouldn't contain simple_fields_for. This partial is used to render the fields of one distinct association object. It is shown when you click 'Add a qualification' or when your parent object already has some qualifications. simple_fields_for :qualifications should be put in you profile form and contain render 'qualifications/qualifications_fields', f: f and link_to_add_association .... Consider this example: https://github.com/nathanvda/cocoon#simpleform

这篇关于Rails-茧形宝石-嵌套表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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