2017-01-27 12 views
0

私は現在仕事用ツールを構築中です。毎日複数のジョブの開始時間と終了時間を提出する必要があります。いくつかの仕事があり、毎日新しいランタイムがあります。だからプロセスをスピードアップするために、すべての実行時間を1つの形式で提出したいと考えています。現在私はすべてのフォームを表示していますが、提出するときには1つの提出物しか通過しません。私は何が欠けていますか?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を使用する必要があります。 [このSOの質問]を見てください(http://stackoverflow.com/questions/8563299/submit-multiple-forms-with-one-submit-button) – chester

+0

また、 '@ runtimes'インスタンス変数を作成しますか?あなたは必要なフォームの数を作成するために繰り返し処理する配列を持っている?あなたは単に '@mrpjobs.each do ... end'を使うことができます。 – chester

+0

@chester私が調べたいくつかのことを試した後、それを削除するのを忘れてしまった。私はそれを変えて、家に帰るときにアヤックスを働かせることができるかどうかを見ます。助けてくれてありがとう –

答えて

0

は、Ajaxについてのコメントを無視し、fields_forあなたがやりたいだろうし、このための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 %> 

_form.html.erbform_forを削除するために変更する必要があります。次に、#update_batchのような複数の@runtimeパラメータを同時に処理できる新しいコントローラアクションを作成する必要があります。

これはすべての詳細ではありませんが、それはアイデアです。あなたは何をしたいのですか?のすべてのパラメタを一度にランタイムのを持つ1つのフォームの投稿です。

+0

私はそれも動作すると思ったが、私はそれを試してみると、1つの送信ボタンは何もせず、複数のレンダリングは、1つだけを提出している。 –

+0

ええ、すべてのフィールドが1つのフォーム内にある必要があります。ブラウザは一度に1つのフォームのみを送信します。また、フィールドには一意の名前が必要です。 1つのフォームを使用するように変更すると、送信されるパラメータは何ですか? –

関連する問題