Rails Simple_form - 编辑 + 每次点击时显示页面复制嵌套表单
Rails Simple_form - edit + show page duplicating nested form every time Its clicked
我正在开发一个基本的 Web 应用程序,其中一个要求是具有嵌套表单的配置文件页面。嵌套表单是一个称为体验的模型,它基本上询问用户是否有以前的工作经验,并询问有关其工作经验的基本信息。这个嵌套表单需要能够被复制,以便用户可以根据需要添加任意数量的表单,这就是我使用 nested_form gem.
的原因
我遇到的问题是,每次我单击编辑按钮时,表单本身似乎都会被复制,并且在几次之后我最终将嵌套表单重复 5 或 6 次而无法删除它们(尽管添加了 nested_form gem 附带的删除 link)。我不确定问题是什么,但我假设它与我的控制器或我在 _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 简介 < 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
如果我理解正确,当前 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
才能使 Delete 生效。
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
我正在开发一个基本的 Web 应用程序,其中一个要求是具有嵌套表单的配置文件页面。嵌套表单是一个称为体验的模型,它基本上询问用户是否有以前的工作经验,并询问有关其工作经验的基本信息。这个嵌套表单需要能够被复制,以便用户可以根据需要添加任意数量的表单,这就是我使用 nested_form gem.
的原因我遇到的问题是,每次我单击编辑按钮时,表单本身似乎都会被复制,并且在几次之后我最终将嵌套表单重复 5 或 6 次而无法删除它们(尽管添加了 nested_form gem 附带的删除 link)。我不确定问题是什么,但我假设它与我的控制器或我在 _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 简介 < 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
如果我理解正确,当前 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
才能使 Delete 生效。
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