Rails:从下拉菜单中选择选项,删除超过x个月的记录

时间:2016-08-17 20:46:59

标签: ruby-on-rails json

所以我一直在创建具有Pings表的rails应用程序。它有一个带有下拉菜单的管理页面,可选择1个月,2个月,3个月,4个月,5个月。

选择其中一个选项,比如2个月,我希望删除Ping表中超过2个月的所有记录。我一直试图在过去2天内完成这项任务,但并未取得成功。谁能帮我这个?提前谢谢。

rails_app
  views
    pings
        _form.html.erb
        admin.html.erb
        edit.html.erb
        index.html.erb
        new.html.erb
        show.html.erb
        show.html.erb

这是ping控制器。

class PingsController < ApplicationController
    before_action :set_ping, only: [:show, :edit, :update, :destroy]


    # GET /pings
    # GET /pings.json
    def index
      @pings = Ping.all
    end

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

    # GET /pings/new
    def new
      @ping = Ping.new
    end

    # GET /pings/1/edit
    def edit
    end

    # POST /pings
    # POST /pings.json
    def create
      @ping = Ping.new(ping_params)

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

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

    # DELETE /pings/1
    # DELETE /pings/1.json
    def destroy
      @ping.destroy
      respond_to do |format|
        format.html { redirect_to pings_url, notice: 'Ping was successfully destroyed.' }
        format.json { head :no_content }
      end
    end

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

      # Never trust parameters from the scary internet, only allow the white list through.
      def ping_params
        params.require(:ping).permit(:response_time, :created_date, :updated_date, :status, :server_id)
      end
  end

这是观点:

            <!--<div class="login-container"> -->
            <div class="container">
                <div class="row">
                    <p id="notice"><%= notice %></p>
                    <div class="col-md-6">
                        <table class="table ">
                        <tr >
                          <td colspan=2><h1>Delete records older than:</h1></td>
                        </tr>
                        <tr>
                            <td align="center"><%= select_tag "duration", options_for_select([['1 month' ,1], ['2 months' ,2], ['3 months' ,3], ['4 months' ,4], ['5 months' ,5]]) %></td>
                            <td><%= link_to 'Delete', root_path, class: 'btn btn-danger' %></td>
                        </tr>
                        </table>
                    </div>
                    <div class="col-md-6">
                        <table class="table ">
                        <tr >
                          <td colspan=3><h1>Set Server ping rate</h1></td>
                        </tr>
                        <tr>
                            <td align="center"><%= link_to '5 minutes', root_path, class: 'btn btn-danger' %></td>
                            <td align="center"><%= link_to '6 Days', root_path, class: 'btn btn-danger' %></td>
                            <td align="center"><%= link_to '10 Days', root_path, class: 'btn btn-danger' %></td>
                        </tr>
                        </table>
                    </div>
                </div>

            </div>

0 个答案:

没有答案