使用.increment方法时,控制器不会更新数据库中的值

时间:2019-02-23 16:36:37

标签: ruby-on-rails

我有一个Rails控制器,当我调用.increment!方法时,它不会更新数据库中的值。

有趣的是,相同的代码也可以在create方法中使用

这是控制器代码

class RequestsController < ApplicationController
  before_action :set_request, only: [:show, :edit, :update, :destroy]

  # GET /requests
  # GET /requests.json
  def index
    @requests = Request.all
  end

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

  # GET /requests/new
  def new
    @request = Request.new
  end

  # GET /requests/1/edit
  def edit
  end

  # POST /requests
  # POST /requests.json
  def create
    @request = Request.new(request_params)
    respond_to do |format|
      if @request.save

        format.html { redirect_to @request, notice: 'Request was successfully created.' }
        format.json { render :show, status: :created, location: @request }

        @request.increment!(:voteCount)

      else
        format.html { render :new }
        format.json { render json: @request.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /requests/1
  # PATCH/PUT /requests/1.json
  def update
    respond_to do |format|
      if @request.update(request_params)
        format.html { redirect_to @request, notice: 'Request was successfully updated.' }
        format.json { render :show, status: :ok, location: @request }

      else
        format.html { render :edit }
        format.json { render json: @request.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /requests/1
  # DELETE /requests/1.json
  def destroy
    @request.destroy
    respond_to do |format|
      format.html { redirect_to requests_url, notice: 'Request was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def request_params
      params.require(:request).permit(:artist, :title, :voteCount)
    end

    def upvote
      puts 'upvote'
      request = Request.find(params[:request_id])

      render json: { voteCount: request.voteCount }
      @request.increment!(:voteCount)
      request.increment!(:voteCount)
      request.save
      request.reload

  end


end

这是html

<tbody>
  <% @requests.each do |request| %>
 <tr data-request-id="<%= request.id %>">
   <td id="artist" style="text-align: center"><%= request.artist %></td>
   <td id="songTitle" style="text-align: center"><%= request.title %></td>
   <td style="text-align: center" id="voteCount<%= request.id %>"><%= request.voteCount %></td>
    <td style="text-align: center"><%= button_to 'Vote', request_upvote_path(request.id), remote: true, method: :post, onclick: "upvote(#{request.id})", class: 'upvote', id:"voteButton#{request.id}" %></td> </tr>
   <% end %>

这是JS,它可以更新页面上的计数

$("#ClickMe").attr("disabled", "disabled"); 



function upvote(id) {
    var count = document.getElementById("voteCount" + id).innerHTML;
    count = parseInt(count);
    count = count + 1;
    count = count.toString();
    document.getElementById("voteCount" + id).innerHTML = count;
    document.getElementById("voteButton" + id).disabled = true;
 }

添加路线.rb

Rails.application.routes.draw do
  resources :requests do
    post 'upvote', to: 'requests#upvote'
  end
end

1 个答案:

答案 0 :(得分:0)

在回答您的问题之前,我有2个问题

  1. 代码在运行时是否返回任何错误?
  2. 您为什么有两个increment!方法?
  3. 您的ERB模板中有@requests,这是有问题的,因为您的控制器中没有@requests实例变量。

.increment!是正确的投票方法;但是,您需要保存它,因为

  

attribute被更新;记录本身不会保存。

因此,您需要将记录保存到数据库

def upvote
  @request = Request.find(params[:request_id])
  @request.increment!(:voteCount)
  @request.save

  render json: { voteCount: @request.voteCount }
end

有关该主题的可能链接: