Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.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
Ruby on rails Rails:如何将多个表单提交到同一个表_Ruby On Rails_Ruby_Forms_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails Rails:如何将多个表单提交到同一个表

Ruby on rails Rails:如何将多个表单提交到同一个表,ruby-on-rails,ruby,forms,ruby-on-rails-3,Ruby On Rails,Ruby,Forms,Ruby On Rails 3,我目前正在构建一个工作工具。它涉及每天提交多个作业开始和结束时间。有几个作业,每个作业每天都有一个新的运行时间。因此,为了加快进程,我希望以一种形式提交所有运行时。目前,我有所有的表格出现,但当我提交只有一个提交通过。我错过了什么 runtime.rb class Runtime < ApplicationRecord belongs_to :Mrpjob accepts_nested_attributes_for :Mrpjob end class RuntimesContro

我目前正在构建一个工作工具。它涉及每天提交多个作业开始和结束时间。有几个作业,每个作业每天都有一个新的运行时间。因此,为了加快进程,我希望以一种形式提交所有运行时。目前,我有所有的表格出现,但当我提交只有一个提交通过。我错过了什么

runtime.rb

class Runtime < ApplicationRecord
  belongs_to :Mrpjob
  accepts_nested_attributes_for :Mrpjob
end
class RuntimesController < ApplicationController
  before_action :set_runtime, only: [:show, :edit, :update, :destroy]

  # GET /runtimes
  # GET /runtimes.json
  def index
    @runtimes = Runtime.all
    @sorting = @runtimes.order("date asc")
  end

  # GET /runtimes/1
  # GET /runtimes/1.json
  def show
  end

  # GET /runtimes/new
  def new
    @runtime = Runtime.new
    @mrpjobs = Mrpjob.all
    @runtimes = Array.new(Mrpjob.count)
  end

  # GET /runtimes/1/edit
  def edit
    @mrpjobs = Mrpjob.all
  end

  # POST /runtimes
  # POST /runtimes.json
  def create
    @runtime = Runtime.new(runtime_params)


    respond_to do |format|
      if @runtime.save
        format.html { redirect_to @runtime, notice: 'Runtime was successfully created.' }
        format.json { render :show, status: :created, location: @runtime }
      else
        format.html { render :new }
        format.json { render json: @runtime.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /runtimes/1
  # PATCH/PUT /runtimes/1.json
  def update
    respond_to do |format|
      if @runtime.update(runtime_params)
        format.html { redirect_to @runtime, notice: 'Runtime was successfully updated.' }
        format.json { render :show, status: :ok, location: @runtime }
      else
        format.html { render :edit }
        format.json { render json: @runtime.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /runtimes/1
  # DELETE /runtimes/1.json
  def destroy
    @runtime.destroy
    respond_to do |format|
      format.html { redirect_to runtimes_url, notice: 'Runtime was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_runtime
      @runtime = Runtime.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def runtime_params
      params.require(:runtime).permit(:start_time, :end_time, :date, :Mrpjob_id)
    end
end
<%= form_for @runtime, :html => { :class => "form-horizontal runtime" } do |f| %>

  <% if @runtime.errors.any? %>
    <div id="error_expl" class="panel panel-danger">
      <div class="panel-heading">
        <h3 class="panel-title"><%= pluralize(@runtime.errors.count, "error") %> prohibited this runtime from being saved:</h3>
      </div>
      <div class="panel-body">
        <ul>
        <% @runtime.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
        </ul>
      </div>
    </div>
  <% end %>
  <div class="col-sm-6 padding">

    <div class="form-group">
      <%= f.label :start_time, :class => 'control-label col-lg-2' %>
      <div class="col-lg-6">
        <%= f.text_field :start_time, :class => 'form-control' %>
      </div>
      <%=f.error_span(:start_time) %>
    </div>
    <div class="form-group">
      <%= f.label :end_time, :class => 'control-label col-lg-2' %>
      <div class="col-lg-6">
        <%= f.text_field :end_time, :class => 'form-control' %>
      </div>
      <%=f.error_span(:end_time) %>
    </div>
    <div class="form-group">
      <%= f.label :date, :class => 'control-label col-lg-2' %>
      <div class="col-lg-6">
        <%= f.text_field :date, :class => 'form-control' %>
      </div>
      <%=f.error_span(:date) %>
    </div>

     <div class="row">
        <% @mrpjobs.each do |p| %>
          <div class="col-sm-2 text-center">
            <%= f.radio_button :Mrpjob_id, p.id %>
            <%= f.label :Mrpjob_id, p.name %>
          </div>
        <% end %>
    </div>

    <div class="form-group">
      <div class="col-lg-offset-2 col-lg-10">
        <%= f.submit nil, :class => 'btn btn-primary' %>
        <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
                  runtimes_path, :class => 'btn btn-default' %>
      </div>
    </div>
  </div>

<% end %>
<%- model_class = Runtime -%>
  <div class="page-header">
    <h1><%=t '.title', :default => [:'helpers.titles.new', 'New %{model}'], :model => model_class.model_name.human.titleize %></h1>
  </div>

  <div class="container">
    <div class="row">

        <% @runtimes.each do |runtime| %>
            <%= fields_for @runtime do |r| %>
              <%= render "form" %>
            <% end %>
        <% end %>
    </div>
  </div>
类运行时
runtimes\u controller.rb

class Runtime < ApplicationRecord
  belongs_to :Mrpjob
  accepts_nested_attributes_for :Mrpjob
end
class RuntimesController < ApplicationController
  before_action :set_runtime, only: [:show, :edit, :update, :destroy]

  # GET /runtimes
  # GET /runtimes.json
  def index
    @runtimes = Runtime.all
    @sorting = @runtimes.order("date asc")
  end

  # GET /runtimes/1
  # GET /runtimes/1.json
  def show
  end

  # GET /runtimes/new
  def new
    @runtime = Runtime.new
    @mrpjobs = Mrpjob.all
    @runtimes = Array.new(Mrpjob.count)
  end

  # GET /runtimes/1/edit
  def edit
    @mrpjobs = Mrpjob.all
  end

  # POST /runtimes
  # POST /runtimes.json
  def create
    @runtime = Runtime.new(runtime_params)


    respond_to do |format|
      if @runtime.save
        format.html { redirect_to @runtime, notice: 'Runtime was successfully created.' }
        format.json { render :show, status: :created, location: @runtime }
      else
        format.html { render :new }
        format.json { render json: @runtime.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /runtimes/1
  # PATCH/PUT /runtimes/1.json
  def update
    respond_to do |format|
      if @runtime.update(runtime_params)
        format.html { redirect_to @runtime, notice: 'Runtime was successfully updated.' }
        format.json { render :show, status: :ok, location: @runtime }
      else
        format.html { render :edit }
        format.json { render json: @runtime.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /runtimes/1
  # DELETE /runtimes/1.json
  def destroy
    @runtime.destroy
    respond_to do |format|
      format.html { redirect_to runtimes_url, notice: 'Runtime was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_runtime
      @runtime = Runtime.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def runtime_params
      params.require(:runtime).permit(:start_time, :end_time, :date, :Mrpjob_id)
    end
end
<%= form_for @runtime, :html => { :class => "form-horizontal runtime" } do |f| %>

  <% if @runtime.errors.any? %>
    <div id="error_expl" class="panel panel-danger">
      <div class="panel-heading">
        <h3 class="panel-title"><%= pluralize(@runtime.errors.count, "error") %> prohibited this runtime from being saved:</h3>
      </div>
      <div class="panel-body">
        <ul>
        <% @runtime.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
        </ul>
      </div>
    </div>
  <% end %>
  <div class="col-sm-6 padding">

    <div class="form-group">
      <%= f.label :start_time, :class => 'control-label col-lg-2' %>
      <div class="col-lg-6">
        <%= f.text_field :start_time, :class => 'form-control' %>
      </div>
      <%=f.error_span(:start_time) %>
    </div>
    <div class="form-group">
      <%= f.label :end_time, :class => 'control-label col-lg-2' %>
      <div class="col-lg-6">
        <%= f.text_field :end_time, :class => 'form-control' %>
      </div>
      <%=f.error_span(:end_time) %>
    </div>
    <div class="form-group">
      <%= f.label :date, :class => 'control-label col-lg-2' %>
      <div class="col-lg-6">
        <%= f.text_field :date, :class => 'form-control' %>
      </div>
      <%=f.error_span(:date) %>
    </div>

     <div class="row">
        <% @mrpjobs.each do |p| %>
          <div class="col-sm-2 text-center">
            <%= f.radio_button :Mrpjob_id, p.id %>
            <%= f.label :Mrpjob_id, p.name %>
          </div>
        <% end %>
    </div>

    <div class="form-group">
      <div class="col-lg-offset-2 col-lg-10">
        <%= f.submit nil, :class => 'btn btn-primary' %>
        <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
                  runtimes_path, :class => 'btn btn-default' %>
      </div>
    </div>
  </div>

<% end %>
<%- model_class = Runtime -%>
  <div class="page-header">
    <h1><%=t '.title', :default => [:'helpers.titles.new', 'New %{model}'], :model => model_class.model_name.human.titleize %></h1>
  </div>

  <div class="container">
    <div class="row">

        <% @runtimes.each do |runtime| %>
            <%= fields_for @runtime do |r| %>
              <%= render "form" %>
            <% end %>
        <% end %>
    </div>
  </div>
class RuntimeController
\u form.html.erb

class Runtime < ApplicationRecord
  belongs_to :Mrpjob
  accepts_nested_attributes_for :Mrpjob
end
class RuntimesController < ApplicationController
  before_action :set_runtime, only: [:show, :edit, :update, :destroy]

  # GET /runtimes
  # GET /runtimes.json
  def index
    @runtimes = Runtime.all
    @sorting = @runtimes.order("date asc")
  end

  # GET /runtimes/1
  # GET /runtimes/1.json
  def show
  end

  # GET /runtimes/new
  def new
    @runtime = Runtime.new
    @mrpjobs = Mrpjob.all
    @runtimes = Array.new(Mrpjob.count)
  end

  # GET /runtimes/1/edit
  def edit
    @mrpjobs = Mrpjob.all
  end

  # POST /runtimes
  # POST /runtimes.json
  def create
    @runtime = Runtime.new(runtime_params)


    respond_to do |format|
      if @runtime.save
        format.html { redirect_to @runtime, notice: 'Runtime was successfully created.' }
        format.json { render :show, status: :created, location: @runtime }
      else
        format.html { render :new }
        format.json { render json: @runtime.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /runtimes/1
  # PATCH/PUT /runtimes/1.json
  def update
    respond_to do |format|
      if @runtime.update(runtime_params)
        format.html { redirect_to @runtime, notice: 'Runtime was successfully updated.' }
        format.json { render :show, status: :ok, location: @runtime }
      else
        format.html { render :edit }
        format.json { render json: @runtime.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /runtimes/1
  # DELETE /runtimes/1.json
  def destroy
    @runtime.destroy
    respond_to do |format|
      format.html { redirect_to runtimes_url, notice: 'Runtime was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_runtime
      @runtime = Runtime.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def runtime_params
      params.require(:runtime).permit(:start_time, :end_time, :date, :Mrpjob_id)
    end
end
<%= form_for @runtime, :html => { :class => "form-horizontal runtime" } do |f| %>

  <% if @runtime.errors.any? %>
    <div id="error_expl" class="panel panel-danger">
      <div class="panel-heading">
        <h3 class="panel-title"><%= pluralize(@runtime.errors.count, "error") %> prohibited this runtime from being saved:</h3>
      </div>
      <div class="panel-body">
        <ul>
        <% @runtime.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
        </ul>
      </div>
    </div>
  <% end %>
  <div class="col-sm-6 padding">

    <div class="form-group">
      <%= f.label :start_time, :class => 'control-label col-lg-2' %>
      <div class="col-lg-6">
        <%= f.text_field :start_time, :class => 'form-control' %>
      </div>
      <%=f.error_span(:start_time) %>
    </div>
    <div class="form-group">
      <%= f.label :end_time, :class => 'control-label col-lg-2' %>
      <div class="col-lg-6">
        <%= f.text_field :end_time, :class => 'form-control' %>
      </div>
      <%=f.error_span(:end_time) %>
    </div>
    <div class="form-group">
      <%= f.label :date, :class => 'control-label col-lg-2' %>
      <div class="col-lg-6">
        <%= f.text_field :date, :class => 'form-control' %>
      </div>
      <%=f.error_span(:date) %>
    </div>

     <div class="row">
        <% @mrpjobs.each do |p| %>
          <div class="col-sm-2 text-center">
            <%= f.radio_button :Mrpjob_id, p.id %>
            <%= f.label :Mrpjob_id, p.name %>
          </div>
        <% end %>
    </div>

    <div class="form-group">
      <div class="col-lg-offset-2 col-lg-10">
        <%= f.submit nil, :class => 'btn btn-primary' %>
        <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
                  runtimes_path, :class => 'btn btn-default' %>
      </div>
    </div>
  </div>

<% end %>
<%- model_class = Runtime -%>
  <div class="page-header">
    <h1><%=t '.title', :default => [:'helpers.titles.new', 'New %{model}'], :model => model_class.model_name.human.titleize %></h1>
  </div>

  <div class="container">
    <div class="row">

        <% @runtimes.each do |runtime| %>
            <%= fields_for @runtime do |r| %>
              <%= render "form" %>
            <% end %>
        <% end %>
    </div>
  </div>
{:class=>“表单水平运行时”}do | f |%>
禁止保存此运行时:
'控制标签col-lg-2'> '窗体控件'%> '控制标签col-lg-2'> '窗体控件'%> '控制标签col-lg-2'> '窗体控件'%> “btn btn主节点”%> t(“helpers.links.cancel”), 运行时路径:class=>“btn btn默认值”%>
new.html.erb

class Runtime < ApplicationRecord
  belongs_to :Mrpjob
  accepts_nested_attributes_for :Mrpjob
end
class RuntimesController < ApplicationController
  before_action :set_runtime, only: [:show, :edit, :update, :destroy]

  # GET /runtimes
  # GET /runtimes.json
  def index
    @runtimes = Runtime.all
    @sorting = @runtimes.order("date asc")
  end

  # GET /runtimes/1
  # GET /runtimes/1.json
  def show
  end

  # GET /runtimes/new
  def new
    @runtime = Runtime.new
    @mrpjobs = Mrpjob.all
    @runtimes = Array.new(Mrpjob.count)
  end

  # GET /runtimes/1/edit
  def edit
    @mrpjobs = Mrpjob.all
  end

  # POST /runtimes
  # POST /runtimes.json
  def create
    @runtime = Runtime.new(runtime_params)


    respond_to do |format|
      if @runtime.save
        format.html { redirect_to @runtime, notice: 'Runtime was successfully created.' }
        format.json { render :show, status: :created, location: @runtime }
      else
        format.html { render :new }
        format.json { render json: @runtime.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /runtimes/1
  # PATCH/PUT /runtimes/1.json
  def update
    respond_to do |format|
      if @runtime.update(runtime_params)
        format.html { redirect_to @runtime, notice: 'Runtime was successfully updated.' }
        format.json { render :show, status: :ok, location: @runtime }
      else
        format.html { render :edit }
        format.json { render json: @runtime.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /runtimes/1
  # DELETE /runtimes/1.json
  def destroy
    @runtime.destroy
    respond_to do |format|
      format.html { redirect_to runtimes_url, notice: 'Runtime was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_runtime
      @runtime = Runtime.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def runtime_params
      params.require(:runtime).permit(:start_time, :end_time, :date, :Mrpjob_id)
    end
end
<%= form_for @runtime, :html => { :class => "form-horizontal runtime" } do |f| %>

  <% if @runtime.errors.any? %>
    <div id="error_expl" class="panel panel-danger">
      <div class="panel-heading">
        <h3 class="panel-title"><%= pluralize(@runtime.errors.count, "error") %> prohibited this runtime from being saved:</h3>
      </div>
      <div class="panel-body">
        <ul>
        <% @runtime.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
        </ul>
      </div>
    </div>
  <% end %>
  <div class="col-sm-6 padding">

    <div class="form-group">
      <%= f.label :start_time, :class => 'control-label col-lg-2' %>
      <div class="col-lg-6">
        <%= f.text_field :start_time, :class => 'form-control' %>
      </div>
      <%=f.error_span(:start_time) %>
    </div>
    <div class="form-group">
      <%= f.label :end_time, :class => 'control-label col-lg-2' %>
      <div class="col-lg-6">
        <%= f.text_field :end_time, :class => 'form-control' %>
      </div>
      <%=f.error_span(:end_time) %>
    </div>
    <div class="form-group">
      <%= f.label :date, :class => 'control-label col-lg-2' %>
      <div class="col-lg-6">
        <%= f.text_field :date, :class => 'form-control' %>
      </div>
      <%=f.error_span(:date) %>
    </div>

     <div class="row">
        <% @mrpjobs.each do |p| %>
          <div class="col-sm-2 text-center">
            <%= f.radio_button :Mrpjob_id, p.id %>
            <%= f.label :Mrpjob_id, p.name %>
          </div>
        <% end %>
    </div>

    <div class="form-group">
      <div class="col-lg-offset-2 col-lg-10">
        <%= f.submit nil, :class => 'btn btn-primary' %>
        <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
                  runtimes_path, :class => 'btn btn-default' %>
      </div>
    </div>
  </div>

<% end %>
<%- model_class = Runtime -%>
  <div class="page-header">
    <h1><%=t '.title', :default => [:'helpers.titles.new', 'New %{model}'], :model => model_class.model_name.human.titleize %></h1>
  </div>

  <div class="container">
    <div class="row">

        <% @runtimes.each do |runtime| %>
            <%= fields_for @runtime do |r| %>
              <%= render "form" %>
            <% end %>
        <% end %>
    </div>
  </div>

[:'helpers.titles.new','new%{model}',:model=>model\u class.model\u name.human.titleize%>

忽略关于ajax的评论,
字段将做您想要做的事情,而ajax在复杂性上是一场白费力气的追逐。您只需将表单标记移动到循环的外部

    <% @runtimes.each do |runtime| %>
        <%= fields_for @runtime do |r| %>
          <%= render "form" %>
        <% end %>
    <% end %>

应该改成

    <% @runtimes.each do |runtime| %>
        <%= form_tag %>
          <%= fields_for @runtime do |r| %>
            <%= render "form" %>
          <% end %>
        <% end %> 
    <% end %>

并删除
\u form.html.erb
中的
表单。然后,您必须创建一个新的控制器操作,该操作可以同时处理多个@runtime参数,比如#update_batch


这不是全部的细节,但这就是想法。您想要的是一个表单post,它同时具有所有运行时的参数

您可能正在呈现多个表单,但仅提交您单击了其提交按钮的表单。您需要使用ajax同时提交多个表单。再看一看,您是否创建了
@runtimes
实例变量,只是为了有一个数组来迭代以创建所需的表单数量?您可以只使用
@mrpjobs.each do。。。结束
@chester在尝试了几件我查过的东西后,我忘了把它取下来。我会改变这一点,看看我回家后是否能让Ajax工作。谢谢你的邀请help@chester好吧,那么ajax请求可以正常工作了。也许我做错了什么,但当我尝试用两个条目时,效果非常好。我知道我现在至少需要10个。当我把它增加到六个进行测试时,我并不总是让它们通过测试,最终会出现这个错误“jquery.self-bd7ddd3…js?body=1:10255 POST 500(内部服务器错误)”。这可能是Cloud9的一个限制,但我不确定。@chester我设法让它工作起来了。它无法工作的原因是sqlite在尝试接受这么多请求时被锁定。在摆弄了一段时间后,我放弃了,转而攻读博士后,现在一切正常。谢谢,我想这也行,但当我尝试时,单个提交按钮不起作用,多个渲染只提交一个。是的,所有字段必须在一个表单中,浏览器一次只能提交一个表单。此外,字段必须具有唯一的名称。当您将其更改为使用一个表单时,提交的参数是什么?