Rails 4 - Cocoon 的嵌套属性 gem

Rails 4 - nested attributes with Cocoon gem

我正在尝试使用 Rails 4.

制作一个应用

我将 Cocoon gem 用于简单形式的嵌套形式。

我有一个资格模型,嵌套在一个配置文件模型中。这些协会是:

资格

  belongs_to :profile

简介

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

在我的个人资料表单中,我将资格属性与:

<%= f.simple_fields_for :qualifications do |f| %>

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

在我的资格表格属性中,我有:

<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-8">
                    <%= f.input :pending, :label => "Are you currently studying toward this qualification?" %>
                </div>
            </div>       

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




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

          </div>


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

          </div>


          </div>

</div>  
</div>      

在我的配置文件控制器中,我有:

def new
    @profile = Profile.new
    @profile.qualifications_build
    @profile.build_vision
    @profile.build_personality
    @profile.addresses_build
    authorize @profile

  end

  # GET /profiles/1/edit
  def edit
    @profile.build_personality unless @profile.personality
    @profile.qualifications_build unless @profile.qualifications
    @profile.build_vision unless @profile.vision
    @profile.addresses_build unless @profile.addresses
  end

  # POST /profiles
  # POST /profiles.json
  def create
    @profile = Profile.new(profile_params)
    authorize @profile

    respond_to do |format|
      if @profile.save
        format.html { redirect_to @profile }
        format.json { render :show, status: :created, location: @profile }
      else
        format.html { render :new }
        format.json { render json: @profile.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /profiles/1
  # PATCH/PUT /profiles/1.json
  def update

    # successful = @profile.update(profile_params)

    # Rails.logger.info "xxxxxxxxxxxxx"
    # Rails.logger.info successful.inspect
    # user=@profile.user
    # user.update.avatar
    respond_to do |format|
      if @profile.update(profile_params)
        format.html { redirect_to @profile }
        format.json { render :show, status: :ok, location: @profile }
      else
        format.html { render :edit }
        format.json { render json: @profile.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /profiles/1
  # DELETE /profiles/1.json
  def destroy
    @profile.destroy
    respond_to do |format|
      format.html { redirect_to profiles_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_profile
      @profile = Profile.find(params[:id])
      authorize @profile
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def profile_params
      params.require(:profile).permit( :title, :hero, :overview, :research_interest, :occupation, :external_profile,
        :working_languages, :tag_list,
          industry_ids: [],
          user_attributes: [:avatar],
          personality_attributes: [:average_day, :fantasy_project, :preferred_style],
          vision_attributes: [ :long_term, :immediate_challenge], 
          qualifications_attributes: [ :level, :title, :year_earned, :pending, :institution, :_destroy],
          addresses_attributes: [:id, :unit, :building, :street_number, :street, :city, :region, :zip, :country, :time_zone, :latitude, :longitude, :_destroy],
          industries_attributes: [:id, :sector, :icon] )
    end
end

我的问题是:

  1. 当我保存所有这些并尝试时,每次我更新配置文件时,它都会为资格创建一个新条目,因此资格列表在每次更新时都会显示重复项(即使没有对字段进行了更新)。

  2. 当我更新配置文件表单并单击删除资格时,它并没有从数组中删除。我可以在控制台中删除它们,但不能使用表单。

我担心如果我删除

@profile.qualifications_build unless @profile.qualifications

通过配置文件控制器中的编辑操作,我将无法更新这些属性。

另外,我觉得删除它就可以解决删除资格的问题,这对我来说没有意义。我是否需要在删除限定条件中添加一些内容 link 以强制它删除条目?

我想你只是忘了在允许的资格参数中添加 :id,在你的 profile_params:

qualifications_attributes: [ :id, :level, :title, :year_earned, :pending, :institution, :_destroy]

没有ID匹配,任何资格都将被视为新资格。