大家好,我目前正在开发我的应用程序中的一个部分,使设计者可以使用过去的工作经验和教育来填写表格。我正在使用嵌套属性来创建工作记录和教育记录,效果很好。但是我希望能够以相同的形式添加不止一种工作经验和不止一种教育
我尝试使用嵌套属性,并尝试在提交表单时创建2条记录,但创建的记录却对雇主具有相同的值
user.rb
has_many :work_exps
has_many :educations
accepts_nested_attributes_for :work_exps
accepts_nested_attributes_for :educations
experience.html.erb
<%= form_for :user, url: 'experience' do |f|%>
<h3>work experience:</h3>
<%= fields_for :work_exps do |u| %>
<%= u.label :employer %><br />
<%= u.text_field :employer %><br>
<%= u.label :employer %><br />
<%= u.text_field :employer %>
<% end %>
<%= fields_for :educations do |u| %>
<%= u.label :school %><br />
<%= u.text_field :school %>
<% end %>
路线
post 'care_giver_steps/experience', to: 'care_giver_steps#experience'
控制器
def new
@user = current_user
@user.work_exps.build
@user.educations.build
render_wizard
end
def experience
@user = current_user
2.times{@user.work_exps.create(sanitize_work)}
@user.educations.create(sanitize_education)
redirect_to team_root_path
end
private
def sanitize_work
params.require(:work_exps).permit(:employer,:title)
end
def sanitize_education
params.require(:educations).permit(:major,:school)
end
答案 0 :(得分:0)
def new
@user = current_user
2.times{@user.work_exps.build}
@user.educations.build
render_wizard
end
def experience
@user = current_user
@user.update(sanitize_params)
redirect_to team_root_path
end
private
def sanitize_params
params.require(:user).permit(work_exps_attributes: [:employer,:title], educations_attributes: [:major,:school])
end
routes.rb
post 'care_giver_steps/experience', to: 'care_giver_steps#experience', as: :care_giver_steps_experience
experince.html.erb
<%= form_for :user, url: care_giver_steps_experience_path do |f|%>
<h3>work experience:</h3>
<%= fields_for :work_exps do |u| %>
<%= u.label :employer %><br />
<%= u.text_field :employer %><br>
<%= u.label :employer %><br />
<%= u.text_field :employer %>
<% end %>
<%= fields_for :educations do |u| %>
<%= u.label :school %><br />
<%= u.text_field :school %>
<% end %>