Rails 4-Cocoon gem的嵌套属性 [英] Rails 4 - nested attributes with Cocoon gem

查看:69
本文介绍了Rails 4-Cocoon gem的嵌套属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to make an app with Rails 4.

我正在将Cocoon gem用于具有简单形式的嵌套形式.

I'm using Cocoon gem for nested forms with simple form.

我有一个资格模型,嵌套在个人资料模型中.关联是:

I have a qualifications model, nested in a profile model. The associations are:

资格

  belongs_to :profile

个人资料

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

在我的个人资料表格中,我将资格条件属性与以下内容合并在一起:

In my profile form, I incorporate the qualifications attributes with:

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

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

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

In my Qualifications form attributes, I have:

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

在我的个人资料控制器中,我有:

In my profiles controller I have:

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. 当我保存所有这些并尝试使用它时,每次我更新配置文件时,它都会为资格创建一个新条目,因此,每次更新都会显示重复的资格列表(即使未进行任何更新)田野).

  1. when I save all of this and try it, each time I update the profile, it creates a new entry for the qualifications, so the list of qualifications displayed duplicates on every update (even when no update is made to the fields).

当我更新配置文件表单并单击删除资格时,它不会从阵列中删除.我可以在控制台中删除它们,但不能使用表单.

When I update the profile form and click remove qualification, it does not get removed from the array. I can delete them in the console, but not using the form.

我担心如果删除

@profile.qualifications_build unless @profile.qualifications

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

from the edit action in the profile controller, I won't be able to update those attributes.

另外,对我来说,删除将解决删除资格问题没有任何意义.我是否需要在删除资格链接中添加一些内容以强制其删除条目?

Also, it doesn't make sense to me that removing that would solve the problem with removing qualifications. Do I need to add something to the remove qualification link to force it to remove the entry?

推荐答案

我认为您只是忘记在profile_params的资格条件允许的参数中添加:id:

I think you just forgot to add :id in the allowed parameters for qualifications, in your profile_params:

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

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

Without an ID to match, any qualification will be seen as a new one.

这篇关于Rails 4-Cocoon gem的嵌套属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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