Ajax追加不起作用于ruby

时间:2017-03-29 19:21:50

标签: javascript jquery ruby-on-rails ruby ajax

我正在试验红宝石上的ajax,这很棒,我几乎击中目标,但最后一步逃脱了我。 一切都在最后一个create.js行之前工作 nb:我在创建动作后运行了一些动作,我认为这可能是问题所在。

我的代码=

class CowsController < ApplicationController
  before_action :authenticate_user! 
  skip_before_action :configure_sign_up_params
  before_action :set_cow, only: [:show, :edit, :update, :destroy]
  after_action :new_cycle, only: [:create]
  after_action :update_cycle, only: [:update]
  after_action :status_cycle, only: [:index]


  # GET /cows
  # GET /cows.json
  def index
    @ranch = current_user.ranches.first
    @cows = @ranch.cows
    @status = [true, false]
    @genders = ([["Mâle", 'f'], ["Femelle", 't']])
    @impacts = ([["Oui", 't'], ["Non", 'f']])
    @cycle = Cycle.new
  end

  # POST /cows
  # POST /cows.json
  def create
    @cow = Cow.new(cow_params)
    @cow.update(ranch_id: current_user.ranches.first.id)
    @cow.update(activ: false)
        if @cow.gender == nil
      @cow.update(gender: false)
    end 
    respond_to do |format|
      if @cow.save
        format.html { redirect_to cows_url, notice: 'Cow was successfully created.' }
        format.js
      else
        format.html { redirect_to cows_path }
        format.json { render json: @cow.errors, status: :unprocessable_entity }
      end
    end
  end

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

  # DELETE /cows/1
  # DELETE /cows/1.json
  def destroy
    @cow.destroy
    respond_to do |format|
      format.html { redirect_to :back, notice: 'Cow was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def cow_params
      params.require(:cow).permit(:ranch_id, :gender, :num, :name, :father, :mother, :activ, :buy_date, :buy_price, :sell_date, :sell_price, :born_date)
    end

    def update_cycle
      UpdateFirstCycleJob.perform_later(@cow)
    end

    def new_cycle
      FirstCycleJob.perform_later(@cow)
    end

    def status_cycle
      @cows.each do |cow|
        StatusCowJob.perform_later(cow)
        EventCowJob.perform_later(cow)
      end
    end


    def set_cycle 
      @cycle = Cycle.find(params[:cycle_id])
    end 


    def cycle_params
      params.require(:cycle).permit(:cow_id, :cat, :start_at, :end_at, :father, :wait, :current_state, :active_e, :current_event, :scurrent_satus_label)
    end

end

牛指数:

<div class="right_col" role="main" >

<h1>Votre troupeau</h1>

<%= render 'new' %>

<%- if @ranch.cows.any? %>
 <!-- Header table (tab) -->
    <div class="btn-pref btn-group btn-group-justified btn-group-lg" role="group" aria-label="...">
      <%- @status.each do |status| %>
        <div class="btn-group" role="group">
          <button type="button" class="btn btn-primary" data-target="#Cow_tab<%=status%>" data-toggle="tab" style="height:40px;">
            <div class="hidden-xs">
              <% if status == true %>
                <i class="fa fa-check-circle" aria-hidden="true"></i>
                En production 
              <% else %>
                <i class="fa fa-times-circle" aria-hidden="true"></i>
                Improductives
              <% end %>
            </div>
          </button>

        </div>
      <%end%>
    </div>
  <!-- /  Header table (tab) -->

  <!-- Body table  -->
  <div class="well">
    <div class="tab-content">
      <%- @status.each do |status| %>
        <div class="tab-pane fade in active" id="Cow_tab<%=status%>">
            <!-- table -->
            <table class="table table-striped">
              <tbody>
                <tr>
                  <td></td>
                  <td><strong>Etat :</strong></td>
                  <td><strong>Nom :</strong></td>
                  <td><strong>Average :</strong></td>
                  <td></td>

                </tr>
                <%- @cows.where(activ: status).order(current_state: :desc).order(created_at: :desc).each do |c| %>
                  <tr id="Cow_tab2<%=c.activ%>">
                    <td style="width:50px; text-align: center;"> 
                      <div data-toggle='modal' data-target='#MyCycleInfo<%=c.id%>' remote="true" data-dismiss="modal"  >
                        <%- if c.activ == true  %>
                          <div class="pin_activ" style="background-color:#42b72a;">
                          </div>
                        <% elsif c.activ == false && c.wait  %>
                          <div class="pin_activ" style="background-color:#f7b733;">
                          </div>
                        <% elsif c.current_state == 7 %>
                          <div class="pin_activ" style="background-color:#31b0d5;">
                          </div>
                        <% else %>
                          <div class="pin_activ" style="background-color:#fa3e3e;">
                          </div>
                        <% end %>
                      </div>   
                    </td>
                    <td style="width:100px;">
                      <%= c.scurrent_satus_label%>
                    </td>
                    <td><%= c.name %> ( <%= c.num %> )</td>
                    <td><%= c.num %></td>
                    <td style="width:50px; text-align: center;">
                      <%= render 'edit', cow: c %>
                    </td>

                  </tr>

                  <!-- Modal info cycle  -->
                  <div id='MyCycleInfo<%=c.id%>' class='modal fade' role='dialog' aria-hidden="true">
                    <div class="modal-dialog">
                      <div class='content'>

                        <div class="modal-header">
                          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                          <%= render 'cycles/info', cow: c %>
                        </div>
                        <div class="modal-body">
                          <%= render 'events/info', cow: c%>
                        </div>
                        <div class="modal-footer" style="text-align:center">
                          <%= render 'historic', cow: c %>
                        </div>
                      </div>
                    </div>
                  </div>
                  <!-- / Modal info cow --> 
                <%end%>
              </tbody>
            </table>
            <!-- / table -->
        </div>
      <%end%>
    </div>
  </div>
  <!-- / Body table  -->

  <h2 style="text-align: center;">Etat du troupeau </h2>

  <%= pie_chart @cows.group(:scurrent_satus_label).count, donut: true %>

<% end %>
</div>

牛_new:

<div data-toggle='modal' data-target='#MyNewCow1' remote="true" data-dismiss="modal" class="btn btn-success" >
  <i class="fa fa-plus" aria-hidden="true"></i> Nouveau
</div>

<%= simple_form_for(Cow.new, remote: true) do |f| %>

  <!-- Modal new cow  -->
  <div id='MyNewCow1' class='modal fade' role='dialog' aria-hidden="true">
    <div class="modal-dialog" style="width:400px;">
      <div class='content'>
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
          <h3 class="modal-title">Nouvelle vache</h3>
        </div>
        <div class="modal-body">
          <div class="form-inputs">

            <div class="form-inputs">
              <div class="well well-sm text-center"> 
                <div class="btn-group" data-toggle="buttons">
                  <% @genders.each do |g|%>
                    <label class="btn btn-default active">
                      <%= f.radio_button :gender, g[1]%>
                      <%= f.label :gender, g[0]%>
                    </label>
                  <%end%>
                </div>
              </div> 
            </div>

            <%= f.input :num %>
            <%= f.input :name %>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-primary" onclick="showModal('MyNewCow2')">Suivant
          </button>
        </div>
      </div>
    </div>
  </div>
  <!-- / Modal new cow --> 


    <!-- Modal new cow  -->
  <div id='MyNewCow2' class='modal fade' role='dialog' aria-hidden="true">
    <div class="modal-dialog" style="width:400px;">
      <div class='content'>
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
          <h3 class="modal-title">Nouvelle vache</h3>
        </div>
        <div class="modal-body">
          <%= f.label :born_date, label:"Date de naissance" %>
              <br>
          <%= f.date_field :born_date, class: "date_box" %>
          <%= f.input :mother, label:"Mère"%>
          <%= f.input :father, label:"Père" %>

          <div id="sell_hide1" class="btn btn-warning"><i class="fa fa-file-text-o" aria-hidden="true" style="margin:5px;"> </i>information d'achat</div>
          <div id="sell_form1" >
            <%= f.label :buy_date, label:"Date d'achat" %>
            <br>
            <%= f.date_field :buy_date, class: "date_box" %>
            <div class="form-inputs">
              <%= f.input :buy_price, label:"Prix d'achat" %>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-primary" onclick="showModal('MyNewCow1')" style="float:left;">Retour</button>
          <%= f.button :submit, "Créer la vache", class: "button_submit" %>
        </div>
      </div>
    </div>
  </div>
  <!-- / Modal new cow -->

  <%= hidden_field :cycle, :cat, :value => 1 %>


  <script>
  function showModal(id) {
    $(".modal").modal('hide');
    $("#" + id).modal(); }
</script>

<script>
  jQuery(document).ready(function () {
  jQuery('#sell_form1').hide();
  jQuery('#sell_hide1').on('click', function (event) {
  jQuery('#sell_form1').toggle();
      });
  });
</script>

<% end %>

最好的部分是create.js.erb =

$('#MyNewCow2').modal('hide');
$('#cow_num').val('');
$('#cow_name').val('');
$('#cow_born_date').val('');
$('#cow_mother').val('');
$('#cow_father').val('');
$('#cow_buy_date').val('');
$('#cow_buy_price').val('');

$('#Cow_tabfalse').append("<%= j render 'cow', cow: @cow %>")

因此,如果您有任何建议来解决此错误,并停止此操作的崩溃,那就太棒了

谢谢,祝你有个美好的一天

0 个答案:

没有答案