2017-01-27 58 views
0

我目前正在構建一個工作工具。它涉及每天提交多個工作開始和結束時間。有幾個工作,每個工作每天都有一個新的運行時間。所以爲了加速這個過程,我想以一種形式提交所有的運行時間。目前我有所有的表格出現,但是當我只提交一個提交時,我錯過了什麼?Rails:如何將多個表單提交到同一張表

runtime.rb

class Runtime < ApplicationRecord 
    belongs_to :Mrpjob 
    accepts_nested_attributes_for :Mrpjob 
end 

runtimes_controller.rb

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.html.erb

<%= 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 %> 

new.html.erb

<%- 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> 
+0

您可能正在渲染多個表單,但您只提交您點擊提交按鈕的表單。您需要使用ajax同時提交多個表單。看看[這太問題(http://stackoverflow.com/questions/8563299/submit-multiple-forms-with-one-submit-button) – chester

+0

另外,你創建'@ runtimes'實例變量只是有一個數組迭代來創建你需要的表單數量?你可以使用'@mrpjobs.each do ... end'。 – chester

+0

@chester在我嘗試了幾件事情後,我忘了將它刪除。我會改變這種情況,看我回家時是否可以讓Ajax工作。謝謝你的幫助 –

回答

0

忽略有關AJAX的評論,fields_for會做你想要什麼,阿賈克斯因爲這是在複雜白費力氣。你只需要將表單標籤移動到循環的外部。

<% @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 %> 

,並清除form_for_form.html.erb。然後你必須創建一個新的控制器動作,它可以一次處理多個@runtime參數,比如#update_batch。

這不是所有的細節,但這是主意。你想要發生的是一個表單文章,其中的所有的參數一次。

+0

我認爲這也可以工作,但是當我嘗試它時,單個提交按鈕什麼也不做,並且多個呈現只提交一個。 –

+0

是的,所有的字段必須在一個表單內,瀏覽器一次只能提交一個表單。此外,這些字段必須具有唯一的名稱。當您將其更改爲使用一種表單時,提交的參數是什麼? –