【问题标题】:Rails Simple_form - edit + show page duplicating nested form every time Its clickedRails Simple_form - 每次点击时编辑+显示复制嵌套表单的页面
【发布时间】:2015-06-22 08:20:58
【问题描述】:

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

我遇到的问题是,每次我单击编辑按钮时,表单本身似乎都会被复制,并且在几次之后,我最终会发现嵌套表单重复 5 或 6 次而无法删除它们(尽管添加 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

个人资料模型:

类配置文件<:base>

    belongs_to :users
    has_many :experiences

    accepts_nested_attributes_for :experiences, allow_destroy: true


end

体验模型:

class Experience < ActiveRecord::Base

    belongs_to :profile 

end

【问题讨论】:

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


    【解决方案1】:

    如果我理解正确,当前nested_form 正在生成新记录,当您点击编辑而不是更新现有的。

    您需要在profile_params 中传递:id 才能使更新 工作。

    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
    

    【讨论】:

    • 感谢您的回复。我尝试添加 id 但仍然有同样的问题。每次我单击编辑时,主窗体中的嵌套窗体都会有另一个重复项。我想要的是,如果nested_form 根本不出现,除非用户单击表单中提供的“添加体验”链接。我怎样才能做到这一点?
    • 好的,我只是仔细检查了一下,似乎添加“id”似乎已经纠正了重复问题。我现在唯一遇到的问题是在将现有记录添加到表单后删除它们。由于某种原因,删除链接似乎不起作用。你知道如何解决这个问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多