Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在为父对象分配ID之前保存嵌套数据_Javascript_Ruby On Rails_Ruby_Ruby On Rails 4 - Fatal编程技术网

Javascript 在为父对象分配ID之前保存嵌套数据

Javascript 在为父对象分配ID之前保存嵌套数据,javascript,ruby-on-rails,ruby,ruby-on-rails-4,Javascript,Ruby On Rails,Ruby,Ruby On Rails 4,我有简历模式和教育模式。教育属于简历,简历有很多教育。 我目前正在简历的show.html.erb视图中使用一个表单来输入教育数据,以确保它能正常工作 在我的routes.rb文件中,我有: resources :resumes do resources :educations end 在我的educations\u controller.rb文件中,我有以下内容: def create @resume = Resume.find(params[:resume_id])

我有简历模式和教育模式。教育属于简历,简历有很多教育。 我目前正在简历的
show.html.erb
视图中使用一个表单来输入教育数据,以确保它能正常工作

在我的
routes.rb
文件中,我有:

resources :resumes do
    resources :educations
end
在我的
educations\u controller.rb
文件中,我有以下内容:

def create
    @resume = Resume.find(params[:resume_id])
    @education = @resume.educations.build(education_params)

    respond_to do |format|
      if @education.save
        format.html { redirect_to @resume, notice: 'Education was successfully created.' }
        format.json { render :show, status: :created, location: @education }
      else
        format.html { render :new }
        format.json { render json: @education.errors, status: :unprocessable_entity }
      end
    end
  end
这允许我在我的
视图/resume/show.html.erb
中拥有以下内容:

<%= form_for [@resume,Education.new] do |f| %>
  <div class="field">
    <%= f.label :sectionTitle %><br>
    <%= f.text_field :sectionTitle %>
  </div>
  <div class="field">
    <%= f.label :completed %><br>
    <%= f.date_select :completed %>
  </div>
  <div class="field">
    <%= f.label :degree %><br>
    <%= f.text_field :degree %>
  </div>
  <div class="field">
    <%= f.label :school %><br>
    <%= f.text_field :school %>
  </div>
  <div class="field">
    <%= f.label :summary %><br>
    <%= f.text_area :summary %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>






这是目前的工作,并允许我进入多个教育每个简历

问题是,我必须先创建简历条目,然后再创建教育条目,因为教育条目取决于简历id整数

我如何重新构建我的代码,以便我可以为尚未具有ID的特定简历创建多个教育条目,以便基本上在提交简历时为其分配ID,然后将任何后续教育附加到给定的简历ID并提供其自己的教育ID


我怀疑我可能必须使用javascript,这很好,但即便如此,我也不确定一旦截获了默认表单操作,该怎么办。

这只是一个小例子,但它完全可以正常工作

在routes.rb中

post 'add_resume_education' => 'resumes#twince'
在resumes_controller.rb中,在您的情况下。使用你自己的
模型
如简历和教育,并更改通过参数

def twince
     resume = Resume.create!(title: params[:data][:category_title])
     post = resume.educations.create!(sectionTitle: params[:data][:educations][:sectionTitle])
     redirect_to :back, notice: 'Yeah Created'
end
在HTML页面中:

<%= form_for :data, url: add_resume_education_path do |f| %>

  <div class="field">
    <%= f.label :category_title %>
    <%= f.text_field :category_title %>
  </div>
<br>
  <div class="field">
    <%= f.fields_for :educations do |edu| %>
     <div class="field">
       <%= edu.label :sectionTitle %><br>
       <%= edu.text_field :sectionTitle %>
     </div>
    <% end %>
  </div>

    <br>
    <%= f.submit 'create' %>
<% end %>






这也是一个很好的源代码,

你想在简历中创建简历和教育吗#用嵌套表单创建方法对吗?是的,虽然嵌套表单不是我目前实现的,但我可以让它工作。然而,我不确定如何在每份简历中创建一个以上的学历。思想?谢谢你的回复。你想如何设计,每个简历下有一个或多个教育背景?是的。一份简历下的许多教育@7URKM3如果我理解正确,在填写表格时,请填写简历数据和教育数据,并在一次提交中创建两者,并将教育作为简历的子项?谢谢。在过去的几个小时里,我一直在与它合作,似乎教育正在创造,但与简历无关。继续尝试。一旦我找到了解决方案,我会发回。只要照我所做的去做,一切都会很完美。它的sipmle@tfer77你有任何验证吗?我目前没有,没有。我想我的问题是这行:education=resume.educations.create!(params[:resume][:education]),在示例中为:post=category.posts.create!(title:params[:category][:posts][:post_title],body:params[:category][:posts][:post_body])我不确定如何编写这一特定行以适合我的示例。有什么指导吗?我认为示例中的title:和body:让我很反感。@tfer77 Resume创建了什么?