Rails 4 - 带有Cocoon gem的嵌套属性

时间:2016-01-11 07:20:23

标签: ruby-on-rails path nested-attributes 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. 当我更新配置文件表单并单击删除限定时,它不会从数组中删除。我可以在控制台中删除它们,但不能使用表单。

  3. 我担心如果我删除

    @profile.qualifications_build unless @profile.qualifications
    

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

    此外,删除它可以解决删除资格问题,对我来说没有意义。我是否需要在删除资格链接中添加内容以强制它删除条目?

1 个答案:

答案 0 :(得分:1)

我想您忘记在:id中的profile_params

中,在允许的资格参数中添加qualifications_attributes: [ :id, :level, :title, :year_earned, :pending, :institution, :_destroy]
event.target

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