Rails使用JavaScript嵌套多态形式 [英] Rails nested polymorphic form with javascripting

查看:79
本文介绍了Rails使用JavaScript嵌套多态形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在表单上输入以下内容:

I want the following on my form:

学位是与个人资料具有has_many关系的多态部分.该部分称为主要形式-轮廓.通过按添加资格",用户可以增加尽可能多的学位.我只能将一个学位附加到个人资料,而最后一个学位则全部被忽略.我什至不知道为什么会这样,因为link_to无法传递配置文件实例.所以我必须在degreescontroller中创建新的配置文件,正如您在我的

Degree is polymorphic partial with has_many relation to profile. This partial is called by main form - profile. By pressing 'Add a Qualification' user can add as many degrees. I've been able to attach only one degree to profile which is the last one others are all ignored. I even know why it's happening because link_to is not able to pass profile instance. so I have to create new profile in degrees_controller as you can see in my code here. Can final profile instance pick up all others above when submitting 'Create Profile'.

请提供任何帮助,以便我可以将所有学位与形式联系在一起,最近几天我一直在坚持使用SO和google的所有排列和组合.我已经准备好更改代码....对此的任何帮助将不胜感激.

Kindly any help so that I can have all of the degrees attached with form, I'm stuck on this for last couple of days with all permutations and combinations from SO and google. I'm ready to change code even....any help with this will be appreciated.

推荐答案

这是一个多态嵌套关联,可以在客户端使用javascript处理.因此,最后对于嵌套字段,我使用了插件 Numerous.js .只需按照链接的qucikstart部分中给出的步骤进行操作,即从Github下载many.js文件并将其保存到资产/javascripts.

This is a polymorphic nested association and can be handled on client side with javascript. So, finally for nested fields I used the plugin Numerous.js. Just follow as the steps given in the qucikstart part of link by downloading the numerous.js file from the Github and saving to assets/javascripts.

在我的代码中,

profile.rb

profile.rb

class Profile < ApplicationRecord
  has_many :degrees, :as => :degreeable
  accepts_nested_attributes_for :degrees, :reject_if => :all_blank, allow_destroy: true

  belongs_to :user, :class_name => 'User', :foreign_key => 'user_id'
end

degree.rb

degree.rb

class Degree < ApplicationRecord
   belongs_to :degreeable, polymorphic: true
end

profiles/_form.html.erb

profiles/_form.html.erb

    <div id="fields-for-list" class="numerous">
      <%= f.fields_for :degrees, Degree.new, :child_index => 'replace_this' do |degree_form| %>
         <%= degree_form.select :level, options_for_select(Job::EDUCATION, params[:level]), include_blank: "Select Degree", :class => 'span5' %>
         <%= degree_form.text_field :description, placeholder: "Add a new Degree here..."%>
         <%= link_to 'x Remove', '#', :class => 'numerous-remove', type: 'button' %>
     <% end %>
   </div>
   <div id="list"></div>
   <%= link_to (fa_icon 'plus').to_s + 'Add a Qualification', '#', :id => 'add-to-list' %>

最后,具有强大的参数,

and finally, with strong parameters,

  def profile_params
    params.require(:profile).permit(:user_id, :first_name, :last_name, degrees_attributes:[:id, :level, :description])
  end

请注意,我已经设置了度数表,其中有2个额外的字段用于多态关联:-"degreeable_id" & "degreeable_type",当在数据库中输入时,这两个字段会自动填充新创建的profile_id和'Profile'(多态与度相关联的模型).

Note that I had already set-up degree table with 2 extra fields for polymorphic association:- "degreeable_id" & "degreeable_type" and when entering in DB, the two fields were automatically filled with newly created profile_id and 'Profile'(the name of the model which is associating polymorphically with degree).

many.js中的技巧是创建具有唯一临时ID(例如Time.now.to_i)的每个嵌套表单记录(此处为度),因此现在在客户端创建/销毁的每个度记录都将具有一个diff degree_attribute'id'.希望对别人有帮助.

The trick in numerous.js is creating each nested form record(here degree) with unique temporary id such as Time.now.to_i so now each degree record created/destroyed at client side will have a diff degree_attribute 'id'. Hope it helps others.

这篇关于Rails使用JavaScript嵌套多态形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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