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

时间:2015-06-22 08:20:59

标签: ruby-on-rails forms nested-forms

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

我遇到的问题是,每当我点击编辑按钮时,表单本身似乎都会被删除,并且在几次之后我最终将嵌套表单重复5或6次而无法删除它们(尽管添加nested_form gem附带的remove链接。我不知道是什么问题,但我假设它与我的控制器或我在_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

个人资料模型:

class Profile&lt;的ActiveRecord ::基

    belongs_to :users
    has_many :experiences

    accepts_nested_attributes_for :experiences, allow_destroy: true


end

体验模型:

class Experience < ActiveRecord::Base

    belongs_to :profile 

end

1 个答案:

答案 0 :(得分:1)

如果我理解正确,当您点击 编辑时,: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 删除 才能正常工作。

java.lang.IncompatibleClassChangeError: org/objectweb/asm/AnnotationVisitor