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

我正在尝试用Rails 4创建一个应用程序。

我正在使用Cocoon gem来创建具有简单forms的嵌套表单。

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

资格

belongs_to :profile 

轮廓

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

在我的个人资料表格中,我将资格属性与以下内容相结合:

    

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

 
"Your award" %>
"Are you currently studying toward this qualification?" %>
"When did you graduate?", collection: (Date.today.year - 50)..(Date.today.year + 5) %>

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

 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. 当我更新配置文件表单并单击删除限定时,它不会从arrays中删除。 我可以在控制台中删除它们,但不能使用表单。

如果我删除,我很担心

 @profile.qualifications_build unless @profile.qualifications 

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

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

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

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

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