繁体   English   中英

Rails Simple_form-编辑+显示页面,每次单击时都会复制嵌套表单

[英]Rails Simple_form - edit + show page duplicating nested form every time Its clicked

我正在开发一个基本的Web应用程序,其中一项要求是带有嵌套表单的Profile页面。 嵌套表单是一种称为体验的模型,该模型基本上询问用户是否有以前的工作经验,并询问有关其工作经验的基本信息。 此嵌套表单需要能够复制,以便用户可以根据需要添加任意数量,这就是为什么im使用nested_form gem的原因。

我遇到的问题是,每次我单击编辑按钮时,表单本身似乎都会被简化,几次后,我最终将嵌套表单重复了5或6次,无法删除它们(尽管添加了remove nested_form gem随附的链接)。 我不确定这个问题是什么,但是我假设是我的控制器还是在_form页面中设置嵌套表单的方式。 请帮忙!

我的代码:-

我的表格:

<%= simple_nested_form_for(@profile) do |f| %>
  <%= f.error_notification %>

     .
     .
     .

    <%= f.input :work_experience, collection: ['No', 'Yes'], label: 'Previous Work Experience?', as: :radio_buttons, :include_blank => false %>
   </div>
  </div>


    <%= f.simple_fields_for :experiences do |builder| %>
        <%= builder.input :company %>
    <%= builder.input :period_of_employment, label: 'Period of Employement:', as: :date, start_year: Date.today.year - 50, end_year: Date.today.year, order: [:day, :month, :year], input_html: { class: 'inline-date' } %>
    <%= builder.input :title, label: 'Job Title' %>
    <%= builder.link_to_remove "Remove" %>

      <% end %>
   <%= f.link_to_add "Add another", :experiences %>


    <div class="form-actions">
      <%= f.button :submit %>
    </div>
<% end %>

配置文件控制器

class ProfilesController < ApplicationController
  before_action :set_profile, only: [:show, :edit, :update, :destroy]

  respond_to :html

  def index
    @profiles = Profile.all
    respond_with(@profiles)
  end

  def show
    respond_with(@profile)
  end

  def new
    @profile = Profile.new
    @profile.experiences.build
    respond_with(@profile)
  end

  def edit
  end

  def create
    @profile = Profile.new(profile_params)
    @profile.user_id = current_user.id
    @profile.save
    respond_with(@profile)
  end

  def update
    @profile.update(profile_params)
    respond_with(@profile)
  end

  def destroy
    @profile.destroy
    respond_with(@profile)
  end

  private
    def set_profile
      @profile = Profile.find(params[:id])
    end

    def profile_params
      params.require(:profile).permit(:name, :civil, :date_of_employment, :mobile, :work_email, :personal_email, :internal_no, :nationality, :gender, :academic_degree, :major, :work_experience, experiences_attributes: [:company, :period_of_employment, :title])
    end
end

经验负责人:

class ExperiencesController < ApplicationController
  before_action :set_experience, only: [:show, :edit, :update, :destroy]

  respond_to :html

  def index
    @experiences = Experience.all
    respond_with(@experiences)
  end

  def show
    respond_with(@experience)
  end

  def new
    @experience = Experience.new
    respond_with(@experience)
  end

  def edit
  end

  def create
    @experience = Experience.new(experience_params)
    @experience.save
    respond_with(@experience)
  end

  def update
    @experience.update(experience_params)
    respond_with(@experience)
  end

  def destroy
    @experience.destroy
    respond_with(@experience)
  end

  private
    def set_experience
      @experience = Experience.find(params[:id])
    end

    def experience_params
      params.require(:experience).permit(:company, :period_of_employment, :title)
    end
end

轮廓模型:

类Profile <ActiveRecord :: Base

    belongs_to :users
    has_many :experiences

    accepts_nested_attributes_for :experiences, allow_destroy: true


end

体验模式:

class Experience < ActiveRecord::Base

    belongs_to :profile 

end

如果我理解正确,那么当您单击edit而不是更新现有记录时, nested_form当前正在生成新记录

您需要在profile_params传递:id才能使更新 profile_params

def profile_params
  params.require(:profile).permit(:id, :name, :civil, :date_of_employment, :mobile, :work_email, :personal_email, :internal_no, :nationality, :gender, :academic_degree, :major, :work_experience, experiences_attributes: [:id, :company, :period_of_employment, :title])
end

更新资料

您需要传递:_destroy才能使删除工作。

def profile_params
   params.require(:profile).permit(:id, :name, :civil, :date_of_employment, :mobile, :work_email, :personal_email, :internal_no, :nationality, :gender, :academic_degree, :major, :work_experience, experiences_attributes: [:id, :company, :period_of_employment, :title, :_destroy])
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM