如何在轨道中使用最佳位置在同一个地方显示创建记录和显示?

时间:2015-09-12 05:38:36

标签: jquery ruby-on-rails ajax

我需要在同一个地方进行内联编辑,创建和更新 的 batch_notifications.controller.rb

   class BatchNotificationsController < ApplicationController
    before_action :set_batch_notification, only: [:show, :edit, :update, :destroy]

    respond_to :html

    def index
      @batch_notification = BatchNotification.new

      @batch_notifications = BatchNotification.all
      @final_count = []
      @calculated_batch_counts = CalculatedBatchCount.all.group_by{|x| x.batch.batch_number  if !x.batch.nil? }
      @a = CalculatedBatchCount.all.group_by{|k| k.batch.serial_id if !k.batch.nil? }

      puts "JAIJAIJAIJAIJAIJAIJAIJAIJAIJAIJAIJAIJAIJAIJAIJAIJAIJAIJAIJAIJAIJAI"
      puts @a.inspect
      @calculated_batch_counts.each do |key, values|
        count = values.map{|x| x.finalCount}.length
        h = {"batch_number" => key, "batch_id" => values.map{|x| x.batch.serial_id},"finalcount" => values.map{|x| x.finalCount}.sum(:+)/count}
        @final_count << h
      end
      # => render :json => @final_count and return
      respond_with(@batch_notifications)
    end


    def show
      respond_with(@batch_notification)
    end

    def new
      @batch_notification = BatchNotification.new


      respond_with(@batch_notification)
    end

    def edit
    end

    def create
      @batch_notification = BatchNotification.new(batch_notification_params)


      respond_to do |format|
        if @batch_notification.save
          format.html { redirect_to :batch_notifications_path, notice: 'Vehicle was successfully created.' }
          format.json { render action: 'index', status: :created, location: @batch_notification }
          format.js
        else
          format.js
          format.html { render action: 'new' }
          format.json { render json: @batch_notification.errors, status: :unprocessable_entity }
        end
      end
    end


    def update
      @batch_notification.update(batch_notification_params)
       respond_to do |format|
         format.js
       end
    end

    def destroy
      @batch_notification.destroy
      respond_with(@batch_notification)
    end

    private
      def set_batch_notification
        @batch_notification = BatchNotification.find(params[:id])
      end

      def batch_notification_params
        params.require(:batch_notification).permit(:message,:approved,:finalCount, :batch_id)
      end
  end

index.html.erb

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>Listing Notifications</h5>
                    <div class="ibox-tools">

                    </div>
                </div>
                <div class="ibox-content">

                    <table class="table table-striped table-bordered" id="batchnote">
                     <thead>
                        <th>Batch</th>
                        <th>Final count</th>
                        <th>Approved</th>
                        <th>Message</th>                        
                   </thead>
                    <tbody>

                            <% @final_count.each do |x| %>        
                      <tr>

                      <td ><%= x["batch_number"] %></td>
                      <td><%= x["finalcount"] %></td>
                        <% @batch = Batch.find_by(:batch_number => x["batch_number"])%>
                     <%= simple_form_for(@batch_notification , remote: true)  do |f| %>

                      <td ><%= f.check_box :approved, label: false, class: "box-control" , :input_html => {value: @batch_notification.approved}%></td>  
                      <td id = "<%= @batch.id %>"  class ="notification_view"><%= f.input :message, label: false, placeholder:"message", class: "form-control" , :input_html => {value: @batch_notification.message}%> </td> 
                      <td><%= f.input :batch_id, as: :hidden, :input_html => {value: @batch.id} %></td>
                      <%end%>


                      </tr>
                    </tbody>
                      <% end %>
                    </table>

                </div>
            </div>
        </div>
    </div>

</div>

batchnotification.rb

 class BatchNotification
  include Mongoid::Document
  include Mongoid::Timestamps
  field :finalCount, type: Float
  field :message, type: String
  field :approved, type: String
  field :batch_id, type: Integer


  belongs_to :batch

end

如何在rails中显示创建和显示在同一个地方。

注意:假设我在消息框中创建消息并按Enter键将显示在同一位置。怎么可能请帮助我如何进行。

0 个答案:

没有答案