首页 > 解决方案 > 使用 .increment 方法时,控制器未更新数据库中的值

问题描述

我有一个 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

标签: ruby-on-rails

解决方案


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

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

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

attribute更新;记录本身没有保存。

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

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

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

关于该主题的可能链接:


推荐阅读